DSA interview question
A logging agent buffers outgoing log lines in a fixed-size array before shipping them, and it needs a first-in, first-out queue: enqueue at the back, dequeue from the front, both O(1). The naive design (dequeue by shifting everything left) is O(n), and "just advance a front index" wastes the freed slots at the start forever. A circular buffer fixes both: keep a head index and a count, and wrap positions around with modulo so freed slots are reused.
Example: enqueue(2), enqueue(3), dequeue() → 2, enqueue(4), dequeue() → 3.
class ArrayQueue {
public:
explicit ArrayQueue(int capacity);
bool enqueue(int x); // false if full
int dequeue(); // -1 if empty
int front() const; // -1 if empty
bool isEmpty() const;
int size() const;
};
| Operations | Output | Explanation |
|---|---|---|
enqueue(2) enqueue(3) dequeue() enqueue(4) dequeue() | 2 3 | First in, first out |
dequeue() on an empty queue | -1 | |
capacity 2: enqueue(1) enqueue(2) enqueue(3) | true true false | Full |
capacity 3: enqueue 1, 2, 3; dequeue; enqueue 4; dequeue ×3 | 1 2 3 4 | 4 wraps into the freed slot 0 |
enqueue(9) front() size() | 9 1 |
(Confirm: all operations O(1); capacity is fixed; −1 signals "empty".)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in