Data Structures & AlgorithmsModerateTCS NQT · Accenture pattern
A circular queue uses an array of size 5 (indices 0–4); front is the index of the first element and rear is the index of the last element. If front = 3 and rear = 1 and the queue is not empty, how many elements does it hold?
- A. 4
- B. 3
- C. 2
- D. 5
Correct answer: A. 4
The elements occupy 3, 4, 0, 1 (wrapping past the end): 4 elements. Formula: (rear − front + N) mod N + 1 = (1 − 3 + 5) mod 5 + 1 = 4. Using rear − front = −2 or forgetting the +1 gives the other values.
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
- Double hashing in a table of size 11 uses h1(k) = k mod 11 and h2(k) = 7 − (k mod 7); probe i goes to (h1 + i·h2) mod 11. Keys 14, 25, 36…
- How many DISTINCT palindromic substrings (counted as strings, not by position) does 'abaab' have?
- For each element of [4, 10, 5, 8, 20, 15, 3, 12], find the nearest SMALLER element to its LEFT (−1 if none) using a monotonic stack. What…
- The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time. Which of these pop sequences is…
- Top-down merge sort (split until single elements, then merge) sorts an array of 13 elements. How many times is the merge step called?
- Insertion sort (shift larger elements right; one key comparison per inner-loop test) sorts [2, 4, 1, 3, 5] into ascending order. How many…