Data Structures & AlgorithmsHardGATE CS · TCS NQT · Product Company SDE
A circular queue is implemented in an array of size 6 with front = 4 and rear = 0 (rear indexes the last stored element, the queue is non-empty and wraps around). How many elements does it hold?
- A. 5
- B. -4
- C. 3
- D. -3
Correct answer: C. 3
Count = ((rear − front + n) mod n) + 1 = ((0−4+6) mod 6) + 1 = 3.
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 uses the Lomuto partition with the last element as pivot on [32, 14, 29, 16, 25, 12, 11, 13, 31]. After one partition call, at…
- Two sorted lists [10, 16, 17, 27, 29] and [16, 20, 22, 25] are merged using the standard merge step. How many element comparisons are made?
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- What is the postfix (Reverse Polish) form of the infix expression "(c + i) a - j / d", built with the standard stack-based conversion (^…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 1, 2, 2, 2, 1, 1] (values 0, 1, 2 only) in…
- Elevation map [6, 1, 1, 0, 3, 3, 5, 5, 2, 0] (bar width 1). Using the two-pointer / prefix-max approach for trapping rain water, how much…