Stack · Data Structures
13 questions · asked in 8 of 27 years (2000–2026)
MCQ 10 · MSQ 2 · NAT 11-mark 11 · 2-mark 2
2000Questions per year2026
Questions 1–5 of 13
Consider a stack data structure into which we can PUSH and POP records. Assume that each record pushed in the stack has a positive integer key and that all keys are distinct.
We wish to augment the stack data structure with an time MIN operation that returns a pointer to the record with the smallest key present in the stack
1) without deleting the corresponding record, and
2) without increasing the complexities of the standard stack operations.
Which one or more of the following approach(es) can achieve it?
We wish to augment the stack data structure with an time MIN operation that returns a pointer to the record with the smallest key present in the stack
1) without deleting the corresponding record, and
2) without increasing the complexities of the standard stack operations.
Which one or more of the following approach(es) can achieve it?
One or more options are correct.
Answer: (A)
We need an MIN operation, and PUSH and POP must not become slower.
Approach: store with every pushed record a pointer to the record that has the smallest key at or below it in the stack. This pointer is found once, at push time, by comparing the new key with the min pointer of the previous top. PUSH needs one comparison and one pointer copy (). POP only removes the top record. Its stored min pointer goes away with it, so the min is correct again for the new top (). MIN just reads the stored pointer of the top record (). So everything stays .
One global pointer to the minimum fails on POP. If the minimum record is popped, the next smallest cannot be found without scanning the rest of the stack. A sorted auxiliary array needs shifting on PUSH to stay sorted. A min-heap makes PUSH and POP cost instead of . This breaks the rule that the standard operations must not become slower.
Answer: Keep, with every record, a pointer to the record with the smallest key below it.
Approach: store with every pushed record a pointer to the record that has the smallest key at or below it in the stack. This pointer is found once, at push time, by comparing the new key with the min pointer of the previous top. PUSH needs one comparison and one pointer copy (). POP only removes the top record. Its stored min pointer goes away with it, so the min is correct again for the new top (). MIN just reads the stored pointer of the top record (). So everything stays .
One global pointer to the minimum fails on POP. If the minimum record is popped, the next smallest cannot be found without scanning the rest of the stack. A sorted auxiliary array needs shifting on PUSH to stay sorted. A min-heap makes PUSH and POP cost instead of . This breaks the rule that the standard operations must not become slower.
Answer: Keep, with every record, a pointer to the record with the smallest key below it.
Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below.
The value of the expression as per the above rules is _____
The value of the expression as per the above rules is _____
Answer: 6
Order of precedence: + is highest, then * / is lowest. The operator with the lowest precedence is done last, so / splits the expression into parts:
First part: + is done before *, so .
Second part: Do + first: . Then the operator is right associative, so: .
The operator / is also right associative: .
Answer: 6
First part: + is done before *, so .
Second part: Do + first: . Then the operator is right associative, so: .
The operator / is also right associative: .
Answer: 6
Let S1 and S2 be two stacks. S1 has capacity of 4 elements. S2 has capacity of 2 elements. S1 already has 4 elements: 100, 200, 300, and 400, whereas S2 is empty, as shown below.
Stack S1
Stack S2
Only the following three operations are available:
PushToS2: Pop the top element from S1 and push it on S2.
PushToS1: Pop the top element from S2 and push it on S1.
GenerateOutput: Pop the top element from S1 and output it to the user.
Note that the pop operation is not allowed on an empty stack and the push operation is not allowed on a full stack.
Which of the following output sequences can be generated by using the above operations?
Stack S1
Stack S2
Only the following three operations are available:
PushToS2: Pop the top element from S1 and push it on S2.
PushToS1: Pop the top element from S2 and push it on S1.
GenerateOutput: Pop the top element from S1 and output it to the user.
Note that the pop operation is not allowed on an empty stack and the push operation is not allowed on a full stack.
Which of the following output sequences can be generated by using the above operations?
One or more options are correct.
Answer: (B), (C), (D)
S1 holds 100, 200, 300, 400 (top). S2 can hold only 2 elements.
- 100, 200, 400, 300: to output 100 first, the elements 400, 300 and 200 (3 elements) must all go to S2, but S2 holds only 2. So this is not possible.
- 200, 300, 400, 100: move 400 and 300 to S2. Output 200. Move 300 back and output 300. Move 400 back and output 400. Then output 100. This is possible.
- 400, 200, 100, 300: output 400. Move 300 to S2. Output 200 and 100. Move 300 back and output 300. This is possible.
- 300, 200, 400, 100: move 400 to S2. Output 300 and 200. Move 400 back and output 400. Then output 100. This is possible.
Answer: (B), (C), (D)
- 100, 200, 400, 300: to output 100 first, the elements 400, 300 and 200 (3 elements) must all go to S2, but S2 holds only 2. So this is not possible.
- 200, 300, 400, 100: move 400 and 300 to S2. Output 200. Move 300 back and output 300. Move 400 back and output 400. Then output 100. This is possible.
- 400, 200, 100, 300: output 400. Move 300 to S2. Output 200 and 100. Move 300 back and output 300. This is possible.
- 300, 200, 400, 100: move 400 to S2. Output 300 and 200. Move 400 back and output 400. Then output 100. This is possible.
Answer: (B), (C), (D)
The result evaluating the postfix expression 10 5 + 60 6 / * 8 - is
Answer: (C)
Evaluate the expression step by step:
- Add 10 and 5: 10 5 + 15
- Divide 60 by 6: 60 6 / 10
- Multiply the two results: * 150
- Subtract 8: 8 142
Answer: 142
- Add 10 and 5: 10 5 + 15
- Divide 60 by 6: 60 6 / 10
- Multiply the two results: * 150
- Subtract 8: 8 142
Answer: 142
Consider the following C program:
What is the output of the program for the following input?
5 2 * 3 3 2 + * +
#include < stdio.h >
#define EOF -1
void push (int); /* push the argument on the stack */
int pop (void); /* pop the top of the stack */
void flagError ();
int main ()
{ int c, m, n, r;
while ((c = getchar ()) != EOF)
{ if (isdigit (c) )
push (c);
else if ((c == '+') || (c == '*'))
{ m = pop ();
n = pop ();
r = (c == '+') ? n + m : n*m;
push (r);
}
else if (c != ' ')
flagError ();
}
printf("% c", pop ());
} What is the output of the program for the following input?
5 2 * 3 3 2 + * +
Answer: (B)
The program reads a postfix expression. A digit is pushed (as the number it shows). An operator pops two items, first and then , and pushes . At the end it prints the top of the stack.
Trace for 5 2 * 3 3 2 + * +:
- push 5, push 2: stack is 5, 2
- : pop 2 and 5, push . Stack: 10
- push 3, push 3, push 2. Stack: 10, 3, 3, 2
- : pop 2 and 3, push . Stack: 10, 3, 5
- : pop 5 and 3, push . Stack: 10, 15
- : pop 15 and 10, push . Stack: 25
The value left on the stack is 25.
Note: the code is printed this way in the paper. Read literally, push(c) pushes the character code of each digit (for example 53 for '5', not 5), and printf("% c", ...) prints a character, not a number. The intended reading, used by the key, takes each digit as its value and prints the result as an integer, which gives 25.
Answer: (B)
Trace for 5 2 * 3 3 2 + * +:
- push 5, push 2: stack is 5, 2
- : pop 2 and 5, push . Stack: 10
- push 3, push 3, push 2. Stack: 10, 3, 3, 2
- : pop 2 and 3, push . Stack: 10, 3, 5
- : pop 5 and 3, push . Stack: 10, 15
- : pop 15 and 10, push . Stack: 25
The value left on the stack is 25.
Note: the code is printed this way in the paper. Read literally, push(c) pushes the character code of each digit (for example 53 for '5', not 5), and printf("% c", ...) prints a character, not a number. The intended reading, used by the key, takes each digit as its value and prints the result as an integer, which gives 25.
Answer: (B)
Keep practising