Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
A circular queue is implemented in an array of size 9 with front = 1 and rear = 8 (rear indexes the last stored element, the queue is non-empty and wraps around). How many elements does it hold?
- A. 7
- B. 9
- C. 1
- D. 8
Correct answer: D. 8
Count = ((rear - front + n) mod n) + 1 = ((8-1+9) mod 9) + 1 = 8.
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
- Quicksort with Lomuto partitioning (last element as pivot) sorts [12, 42, 92, 73, 37, 39, 75, 75, 32]. How many swaps happen in total…
- A singly linked list is 95 -> 83 -> 36 -> 42 -> 99 -> 64 -> 72 -> None. The first 5 nodes are reversed in place (the node that was…
- Using a monotonic (increasing) stack to find, for each element of [20, 17, 18, 14, 44, 28], the nearest element to its left that is…
- A monotonic decreasing stack of indices is used to compute the next-greater-element array for [48, 12, 29, 6, 30, 28] (push index i after…
- A singly linked list is 47 -> 24 -> 78 -> 79 -> 98 -> 29 -> 13 -> None. The standard iterative in-place reversal (prev = None; while cur…
- Using the sliding window technique, what is the maximum sum of any 8 consecutive elements in [24, 30, 4, 27, 25, 2, 13, 22, 26, 1]?