Data Structures & AlgorithmsModerateTCS NQT · Infosys pattern
The first k = 4 elements of the queue 1, 2, 3, 4, 5, 6 (front first) are reversed using a stack: dequeue 4 items onto the stack, pop them back into the queue, then move the remaining n − k items from front to back. What is the queue now, front first?
- A. 5, 6, 4, 3, 2, 1
- B. 1, 2, 3, 4, 6, 5
- C. 4, 3, 2, 1, 5, 6
- D. 6, 5, 4, 3, 2, 1
Correct answer: C. 4, 3, 2, 1, 5, 6
After the stack step the queue is 5, 6, 4, 3, 2, 1 (the reversed block went to the back). Rotating the 2 untouched items 5, 6 from front to back gives 4, 3, 2, 1, 5, 6. Forgetting the final rotation leaves 5, 6, 4, 3, 2, 1.
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
- k sorted lists contain N elements in total. They are merged into one sorted list by repeatedly taking the smallest head from a min-heap…
- A deque of indices gives the MINIMUM of every window of size 3 in [4, 2, 12, 3, 8, 1, 7]. What are the window minima?
- A hash table of size 10 uses linear probing. Slots 2, 3, 4, 7 and 8 are occupied and all others are empty (no deletions have happened). An…
- Quickselect with Lomuto partition (pivot = last element) looks for the 3rd smallest element of [7, 2, 9, 4, 3, 8], recursing only into the…
- Binary search finds the index of the minimum of the rotated sorted array [15, 18, 22, 3, 6, 9, 12] using: mid = (lo + hi) / 2; if a[mid] >…
- The sorted array [3, 4, 7, 10, 11] contains some positive integers. What is the 4th positive integer MISSING from it?