Data Structures & AlgorithmsHardGATE CS · DSA
Items K, L, M, N, O are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?
- A. L M K O N
- B. M K L N O
- C. L K M O N
- D. K N O M L
Correct answer: B. M K L N O
Simulate greedily: push until the needed item is on top, then pop. In M K L N O, when K must be popped, L is on top of it (pushed later and not yet popped), so K cannot come out. The other sequences pass the simulation.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- In a singly linked list with only a head pointer, which operation CANNOT be done in O(1) time?
- Run-length encoding: each maximal run is written as the character followed by its length. What is printed? python s = "yyyyyyyyxx" out, i…
- In the binary array [0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1] you may flip at most 2 zeros to ones. What is the length of the longest run…
- Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than…
- Trains occupy a platform from arrival to departure INCLUSIVE, so a train arriving at the same minute another departs needs a separate…