Data Structures & AlgorithmsHardCognizant · Capgemini pattern
A circular queue of size N = 5 keeps one slot empty. What is printed (dequeued values, 'F' for each rejected enqueue, final count)?
A circular queue of size N = 5 keeps one slot empty. What is printed (dequeued values, 'F' for each rejected enqueue, final count)?
#define N 5
int q[N], front = 0, rear = 0;
void enq(int x) { if ((rear + 1) % N == front) { printf("F"); return; }
q[rear] = x; rear = (rear + 1) % N; }
int deq() { int x = q[front]; front = (front + 1) % N; return x; }
/* enq 1..5; a = deq(); b = deq(); enq(6); enq(7); enq(8);
print a, b, the F's, and (rear - front + N) % N */- A. 1 2 F 4
- B. 1 2 FF 4
- C. 1 2 FF 3
- D. 1 2 F 5
Correct answer: B. 1 2 FF 4
With one slot kept empty the queue holds N − 1 = 4 items. Enqueuing 1..5 rejects 5 (first F). After two dequeues there are 2 items; 6 and 7 fit (4 items), and 8 is rejected (second F). The count (rear − front + N) % N is 4.
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
- Keys 50, 700, 76, 85, 92, 73, 101 are inserted into a hash table of size 7 with h(k) = k mod 7 and linear probing. What is the final table…
- Keys 22, 33, 44, 55 go into a table of size 11 with quadratic probing h(k) + i² (mod 11). In which slots do they land, in order? python m…
- What is printed? (stack-based reversal of words) python s = "data structures are fun" st = [] for w in s.split(): st.append(w) out = []…
- Convert the infix expression to postfix (^ is right-associative, higher than and /, which are higher than + and −). What is printed?…
- Evaluate the postfix expression with single-digit operands. What is printed? python st = [] for t in "62/3-42+": if t.isdigit()…
- What is printed? (deque operations) python from collections import deque d = deque([1, 2, 3]) d.appendleft(0); d.append(4) d.rotate(2)…