PPractick

DSA interview question

Introduction to Doubly Linked Lists

Easy · Data structures · Topics: linkedlist, doubly linkedlist

The problem

A web browser's history needs to go both backward and forward from the current page, and a cache needs to remove any entry in O(1) once it has a pointer to it. A singly linked list can do neither well: it can't step backwards, and removing a node requires finding its predecessor. A doubly linked list gives every node a prev pointer as well as next. Your first task: convert an array into a doubly linked list, and traverse it forwards from the head and backwards from the tail, confirming the prev links are right.

Example: [1, 2, 3] → forwards 1 <-> 2 <-> 3, backwards 3 <-> 2 <-> 1.

Function signature

struct DNode { int data; DNode* prev; DNode* next; };
DNode* arrayToDLL(const vector<int>& a);   // returns the head
vector<int> forward(DNode* head);          // head to tail via next
vector<int> backward(DNode* head);         // tail to head via prev

Test cases

Input aForwardBackwardExplanation
[1, 2, 3]1 <-> 2 <-> 33 <-> 2 <-> 1prev links mirror next links
[7]77Head and tail are the same node
[](empty)(empty)head is nullptr
[4, 4, 9]4 <-> 4 <-> 99 <-> 4 <-> 4

(Confirm: head->prev and tail->next are nullptr; backward traversal must be the exact reverse of forward traversal.)

How interviewers phrase it

Clarifying questions to ask

  1. Should I keep a tail pointer?
  2. Circular or linear?
  3. Who frees the memory?

Follow-ups to expect

  1. "What do you gain over a singly linked list?"
  2. "What does it cost?"
  3. "Where is it used?"
  4. "How do you delete a node in O(1)?"
  5. "Complexity?"

Practise questions like this with a real engineer

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 interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in