DSA Tutorial2 Solution
DSA Tutorial2 Solution
1. Given two arrays A and B with n distinct elements within itself, but some elements can be
common in both arrays. write a pseudocode to count the number of elements that are
common in both A and B. Further, identify the worst case scenario, count the number of
primitive operations in the worst case and express the corresponding running times T(n)
of the algorithm using big-O notation.
E.g. A={1,3,5,7,9}, B={2,4,6,7, 9}, Common elements count: 2 (7 & 9)
2. Consider array based implementation of STACK A. Assume that the array's size is 5
elements maximum. Show the contents of the STACK (trace through) at each step, for the
following sequence of operations: Make sure to mention exceptions like empty/full etc.
Operations: PUSH Z, PUSH X, PUSH F, PUSH D, PUSH B, PUSH N, POP,POP, POP,
POP, POP, POP
Operation A[0] A[1] A[2] A[3] A[4] Exception Value of Element at
Condition (if top(t) top
any)
PUSH Z Z 0 Z
PUSH X Z X 1 X
PUSH F Z X F 2 F
PUSH D Z X F D 3 D
PUSH B Z X F D B 4 B
PUSH N Z X F D B Stack full 4 B
POP Z X F D 3 D
POP Z X F 2 F
POP Z X 1 X
POP Z 0 Z
POP -1 -
POP Stack empty -1 -
Suggested Answer: Let’s use stack as an abstract data type to perform reversal.We push
all those elements in the array to the stack and pop allthose elements afterwards and
print so as to get those printed in thereverse order.
For i← 1 to n do
if isStackFull() then
return Error
else
push (A[i])
i← 1
while isStackEmpty() != TRUE do
A[i]← pop()
i←i+1