DSA interview question
A print server receives an unpredictable number of jobs, so its FIFO queue must grow without limit. A singly linked list with two pointers does it in O(1) per operation: front is where jobs leave, and rear is where new jobs are attached. The classic bug is forgetting that removing the last node must also reset rear to null. Otherwise the next enqueue writes through a dangling pointer.
Example: enqueue(2), enqueue(3), dequeue() → 2, enqueue(4), dequeue() → 3.
class LinkedQueue {
public:
void enqueue(int x);
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 | |
dequeue() on an empty queue | -1 | |
enqueue(1) dequeue() enqueue(2) front() | 1 2 | rear must be reset when the queue empties |
enqueue(5) enqueue(6) size() | 2 | |
enqueue(7) dequeue() isEmpty() | 7 true |
(Confirm: O(1) per operation; −1 signals "empty"; no capacity limit; nodes are freed.)
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