Major 1 - Stacks - Type of topics and questions
Major 1 - Stacks - Type of topics and questions
Sample Q) Look at the above code. Draw the stack as it is, at the end of each
loop.
( B ) Code of Stack
In the lectures went through a sequence of codes, gradually improving the code:
(1) A stack of integers- implemented using arrays (2) Adding exceptions (3)
Resizing the array when it is full (4) Making it generic (Using < E > ), and (5)
Adding an interface.
Other than interfaces, I can ask about the other concepts:
Q) Complete the following code, where popTwice, removes two items from the
stack rather than one. (You need to check if there are enough items on the stack,
else thrown an exception. You need to update the value of top.
Q) I can give slight variations like above on the concept of push, pop and peek
(a) How about pushing twice with a fixed sized array implementation as above?
How about pushing k times, where k is an input to the function. (Codes will
generally be fill in the blanks like above, or a 2 or 3 lines you might need to write
to complete the code).
(b) I could also give you slight variations on the peek function.
Q) I can give you a short piece of code (like the above, but it would be a
complete code) and ask you to make it generic.
Q) I can ask you questions on resizing an array (slight variations to what you saw
in class). For instance:
Complete the resize function below. The function receives a user input k, and
grows the array by k times.
( C ) Applications of Stacks
We covered four applications of stacks: (i) Solving a maze (ii) Matching
Parentheses (iii) Calling functions and (iv) Evaluating postfix expressions
Q) I can give an expression of the kind a + (b – c + { d – f + [g + h] – e) + b],
and ask you to draw the stack after reading each of the parentheses (see the
slide that shows such a sequence of stacks).
Q) I could ask you after giving you an expression, of whether it is a valid or not
valid expression. And at what point would the code detect it is not valid. For
instance, in the example above: “After reading ‘)’, the code will pop ‘{‘ from the
stack, and the two parentheses do not match”
Q) Similarly, I can test on function calls. I can give you a few functions, and also
show addresses of certain lines of code, and ask you to draw the sequence of
stacks.
You could be asked to indicate when a certain value is pushed into the stack. For
instance, ‘572’ is pushed onto the stack just before executing “f3();” inside f2.
Q) I can ask you to evaluate a postfix expression, and draw a sequence of stacks
(showing a stack just before and just after reading each operator).
7 4 -3 * 1 5 + / *
Q) I can ask you if a given postfix expression, draw a sequence of stacks, and
determine where does the algorithm detect that the expression is invalid ( Maybe,
at the end the size of the stack is not 1. Or maybe, we need to pop twice after seeing an
operator, but the stack does not have two items in it. Etc. )