






































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







































































DSA Interview Notes CONTENTS
10.10Dynamic Programming................................... 72 10.11Backtracking........................................ 72 10.12Quick Reference: Which Structure/Algorithm to Reach For................ 72
Interview Notes & Revision Handbook
Compiled from a C / C++ Source Code Repository
Linked Lists (Singly, Doubly, Circular) • Stacks • Queues Binary Trees & BST • Graphs (BFS/DFS) • Sorting Algorithms Dynamic Programming • Master Complexity Cheat Sheet
Prepared for Placements, Coding Interviews, LeetCode & GATE Revision
July 18, 2026
A linked list is a linear data structure where each element ( node ) stores its data plus a pointer to the next node. Unlike an array, nodes are not stored contiguously in memory, so insertion and deletion don’t require shifting elements — only pointer updates. The repository implements three flavors: Singly Linked List , Doubly Linked List , and Circular Linked List , each in both C (struct + malloc) and C++ (class + new). The C++ versions are documented in full below since they are marginally cleaner; the C versions use identical logic and are noted under each operation’s Alternative Approach.
2.1 Singly Linked List
The list is tracked with a head pointer (first node) and a tail pointer (last node), which makes push_back O(1) instead of O(n).
Problem Solved Adds a new node containing val to the very front of the list, so it becomes the new head in O(1) time.
Logic Create a new node. If the list is currently empty, the new node is both head and tail. Otherwise, point the new node’s next at the current head, then move head to the new node. Because we never have to walk the list, this operation doesn’t depend on list length.
Time Complexity - Best Case: O(1) - Average Case: O(1) - Worst Case: O(1)
All cases are O(1) because only pointer reassignment is needed — there is no traversal regardless of list size.
Space Complexity O(1) auxiliary space — only one new node is allocated, which is part of the output, not extra working space.
Dry Run Start: list = 2 -> 3 -> NULL (head → node(2), tail → node(3)) Call push_front(1): 1. newNode = Node(1), newNode->next = NULL 2. head is not NULL, so: newNode->next = head → node(1) now points to node(2) 3. head = newNode → head now points to node(1)
Result: 1 -> 2 -> 3 -> NULL
Function Code
DSA Interview Notes CHAPTER 2. LINKED LIST
void push_front ( int val ){ // adding value to front of the linked list Node ***** newNode = new Node ( val ); if( head == NULL ){ head = tail = newNode ; return; } else{ newNode -> next = head ; head = newNode ; } }
Key Points - Classic interview trap: forgetting to handle the empty list case (head == NULL), which requires setting both head and tail to the new node. - Always O(1) — this is the main reason singly linked lists beat arrays for front insertion. - Assumes a tail pointer is maintained; without one, push_front is still O(1) since tail isn’t needed here. - No memory leak here since the old head is simply linked from the new node, not discarded.
Alternative Approach None needed — this is already optimal at O(1). The identical logic appears in linkedlist.c using malloc instead of new; behavior is the same.
Problem Solved Appends a new node containing val to the end of the list in O(1) time (thanks to the maintained tail pointer).
Logic Create the new node. If the list is empty, it becomes both head and tail. Otherwise, link the current tail’s next to the new node, then advance tail to point to it. This avoids walking the whole list to find the last node.
Time Complexity - Best Case: O(1) - Average Case: O(1) - Worst Case: O(1)
Because a tail pointer is maintained, the last node is always known directly — no traversal is required. (Without a tail pointer, this would degrade to O(n).)
Space Complexity O(1) auxiliary space.
Dry Run Start: list = 1 -> 2 -> NULL (tail → node(2)) Call push_back(4): 1. newNode = Node(4) 2. head is not NULL, so: tail->next = newNode → node(2) now points to node(4) 3. tail = newNode → tail now points to node(4)
Result: 1 -> 2 -> 4 -> NULL
DSA Interview Notes CHAPTER 2. LINKED LIST
Function Code
void pop_front (){ // removing value from front of the linked list if( head == NULL ){ cout << "LL is empty \n " ; return; } Node ***** temp = head ; head = head -> next ; temp -> next = NULL ; delete temp ; }
Key Points - Edge case not handled by this code : popping the last remaining node makes head == NULL, but tail is never reset to NULL — leaving tail as a dangling pointer to freed memory. This is a real bug worth mentioning in an interview: any subsequent push_back on a now-empty list would dereference a freed tail. - Always detach temp->next before deleting, so the deleted node doesn’t accidentally keep the rest of the list “reachable” through a stale pointer (defensive style, not strictly required in C++ since delete doesn’t touch next, but is good practice in general). - In interviews, always ask/clarify: “should the function handle the single-node case explicitly?” — here it works correctly for head, but the tail desync is a bug.
Alternative Approach A hardened version should explicitly reset tail when the list becomes empty:
void pop_front (){ if ( head == NULL ) { cout << "LL is empty \n " ; return; } Node ***** temp = head ; head = head -> next ; if ( head == NULL ) tail = NULL ; // keep tail in sync delete temp ; }
This costs nothing extra (still O(1)) and prevents a dangling tail pointer.
Problem Solved Removes the last node of the list and frees it, making the second-to-last node the new tail.
Logic Since this is a singly linked list, there’s no prev pointer, so the only way to find the node before the tail is to walk from head until temp->next == tail. Once found, that becomes the new tail, and the old tail is detached and freed.
Time Complexity - Best Case: O(n) - Average Case: O(n) - Worst Case: O(n)
DSA Interview Notes CHAPTER 2. LINKED LIST
Because a singly linked list has no backward pointers, finding the second-to-last node always requires a full traversal from head — there is no way to reach it directly, even in the best case (unless the list has 0–1 nodes).
Space Complexity O(1) auxiliary space — only pointers are used, no extra data structure.
Dry Run Start: list = 2 -> 4 -> NULL (tail → node(4)) Call pop_back(): 1. temp = head (→ node(2)) 2. Loop: temp->next is node(4), which equals tail, so loop stops immediately. 3. temp->next = NULL → node(2) now points to NULL 4. delete tail → node(4) freed 5. tail = temp → tail now points to node(2)
Result: 2 -> NULL
Function Code
void pop_back (){ // removing value from back of the linked list if( head == NULL ){ cout << "LL is empty \n " ; return; } Node ***** temp = head ; while( temp -> next != tail ){ temp = temp -> next ; } temp -> next = NULL ; delete tail ; tail = temp ; }
Key Points - This is the classic reason doubly linked lists exist — pop_back on a DLL is O(1) because prev is already known, versus O(n) here. - Bug/edge case : if the list has exactly one node (head == tail), the loop condition temp->next != tail is checked on temp == head == tail, so temp->next is NULL, which is not equal to tail (a valid pointer) — the loop would then dereference temp->next->next on a NULL pointer inside the loop body, causing a crash. This single-node case needs a guard (if (head == tail) { delete head; head = tail = NULL; return; }). - Always mention this O(n) vs O(1)-with-DLL trade-off when discussing linked lists in interviews — it’s one of the most common follow-up questions.
Alternative Approach Use a Doubly Linked List instead — since each node has a prev pointer, pop_back becomes: tail = tail->prev; delete tail->next; tail->next = NULL; — O(1) instead of O(n). See the Doubly Linked List section below.
Problem Solved Inserts a new node containing val at a specific 0-indexed position pos in the list, shifting nothing physically (only pointers change) but requiring traversal to reach that position.
DSA Interview Notes CHAPTER 2. LINKED LIST
Alternative Approach For frequent middle insertions, a Doubly Linked List doesn’t change the O(n) traver- sal cost (you still need to reach position pos), but if positions are always near the tail, tracking tail and traversing backward would help. For truly O(log n) positional inssertion, a Skip List or a balanced order- statistics tree would be used instead — well beyond typical interview scope but worth knowing exists.
Problem Solved Finds the 0-indexed position of the first node whose data equals key; returns -1 if not found.
Logic Walk the list from head, comparing each node’s data to key. Return the index the moment a match is found.
Time Complexity - Best Case: O(1) — key is at the head - Average Case: O(n) - Worst Case: O(n) — key is at the tail or absent
Linked lists don’t support random access, so any search must be a linear scan.
Space Complexity O(1) auxiliary space.
Dry Run List: 1 -> 2 -> 3 -> NULL, call search(2) 1. temp = head (node 1), idx = 0 2. Loop check: temp->next != NULL? node(1)->next is node(2), not NULL → enter loop 3. temp->data (1) == key (2)? No. temp = node(2), idx = 1 4. Loop check: node(2)->next is node(3), not NULL → enter loop 5. temp->data (2) == key (2)? Yes → return idx = 1
Result: 1 (correct in this case)
Function Code
int search ( int key ){ // searches if value exists in the linked list and returns index Node ***** temp = head ; int idx = 0 ; while( temp -> next != NULL ){ if( temp -> data == key ){ return idx ; } temp = temp -> next ; idx ++; } return - 1 ; }
DSA Interview Notes CHAPTER 2. LINKED LIST
Key Points — Confirmed Bug The loop condition while (temp->next != NULL) stops the scan one node too early — it never checks the last node (or the only node, in a single-node list) inside the loop body. This was verified by compiling and running the code:
This is exactly the kind of off-by-one bug interviewers probe for — the fix is to loop while temp != NULL instead of temp->next != NULL.
Alternative Approach — Corrected Version
int search ( int key ){ Node ***** temp = head ; int idx = 0 ; while( temp != NULL ){ // FIX: check temp itself, not temp->next if( temp -> data == key ){ return idx ; } temp = temp -> next ; idx ++; } return - 1 ; }
Complexity is unchanged (O(n) worst case), but correctness is restored for the last-node and single-node cases.
Problem Solved Prints every element in the list from head to tail in order, for debugging/visualization.
Logic Start at head and print each node’s data followed by an arrow, advancing until NULL is reached, then print NULL to visually terminate the chain.
Time Complexity - Best Case: O(n) - Average Case: O(n) - Worst Case: O(n)
Every node must be visited exactly once — there’s no way to print the list faster than its length.
Space Complexity O(1) auxiliary space (excluding output).
Dry Run List: 1 -> 2 -> 3 -> NULL → prints 1->2->3->NULL
DSA Interview Notes CHAPTER 2. LINKED LIST
void push_front ( int val ){ Node ***** newNode = new Node ( val ); if( head == NULL ) head = tail = newNode ; else{ newNode -> next = head ; head -> prev = newNode ; head = newNode ; } }
Key Points - Two pointers must be updated on the existing head (prev) in addition to the new node — forgetting head->prev = newNode is the most common DLL bug and silently breaks backward traversal while leaving forward traversal looking fine. - Symmetrical to push_back — good interview question: “write push_back given push_front.”
Alternative Approach Already optimal at O(1); the C version (doublylinkedlist.c) implements identical logic with malloc/raw pointers.
Problem Solved Appends a node to the end of the DLL in O(1) time.
Logic Mirror of push_front: link the new node’s prev to the current tail, set the old tail’s next to the new node, then advance tail.
Time Complexity Best/Average/Worst: O(1)
Space Complexity O(1) auxiliary space.
Dry Run Start: 1 <-> 2 <-> NULL. Call push_back(4): 1. newNode(4) 2. newNode->prev = tail (4’s prev → 2) 3. tail->next = newNode (2 → 4) 4. tail = newNode
Result: 1 <-> 2 <-> 4 <-> NULL
Function Code
DSA Interview Notes CHAPTER 2. LINKED LIST
void push_back ( int val ){ Node ***** newNode = new Node ( val ); if( head == NULL ) head = tail = newNode ; else{ newNode -> prev = tail ; tail -> next = newNode ; tail = newNode ; } }
Key Points - Same O(1) advantage the singly linked list also gets from tracking tail — the real DLL win shows up in pop_back, not here. - Edge case (empty list) correctly handled by assigning both head and tail.
Alternative Approach None needed — already optimal.
Problem Solved Removes the head node from the DLL in O(1) time.
Logic Save the head in temp, advance head to head->next. If the list isn’t now empty, clear the new head’s prev (since it’s the new front, it has no predecessor). Detach and delete the old head.
Time Complexity Best/Average/Worst: O(1)
Space Complexity O(1) auxiliary space.
Dry Run 1 <-> 2 <-> 4 <-> NULL → pop_front(): 1. temp = head (node 1) 2. head = head->next → head = node 2 3. head != NULL → head->prev = NULL 4. delete node 1
Result: 2 <-> 4 <-> NULL
Function Code
DSA Interview Notes CHAPTER 2. LINKED LIST
Time Complexity Best/Average/Worst: O(1) — this is the headline advantage of a DLL over an SLL, where the equivalent operation is O(n).
Space Complexity O(1) auxiliary space.
Dry Run 2 <-> 4 <-> NULL → pop_back(): 1. temp = tail (node 4) 2. tail = tail->prev → tail = node 2 3. tail != NULL → tail->next = NULL 4. delete node 4
Result: 2 <-> NULL
Function Code
void pop_back (){ if( head == NULL ) cout << "DLL is empty" << endl ;
Node ***** temp = tail ; tail = tail -> prev ; if( tail != NULL ) tail -> next = NULL ; temp -> prev = NULL ; delete temp ; }
Key Points - Same missing-return bug as pop_front — calling pop_back() on an empty list dereferences a NULL tail. Always add return; after guard-clause error messages. - This is the single most important DLL vs SLL comparison point in interviews: “why would you use a DLL over an SLL?” → O(1) back-deletion (and O(1) backward traversal) at the cost of extra memory per node for the prev pointer.
Alternative Approach — Corrected Version
void pop_back (){ if( head == NULL ){ cout << "DLL is empty" << endl ; return; // FIX } Node ***** temp = tail ; tail = tail -> prev ; if( tail != NULL ) tail -> next = NULL ; else head = NULL ; // FIX: keep head in sync delete temp ; }
DSA Interview Notes CHAPTER 2. LINKED LIST
Problem Solved Prints the DLL from head to tail, using <-> to visually indicate the bidirectional links.
Logic Standard forward traversal identical in structure to the singly linked list’s print — the prev pointers exist but aren’t needed for a forward print.
Time Complexity Best/Average/Worst: O(n) — every node must be visited.
Space Complexity O(1) auxiliary space.
Dry Run 1 <-> 2 <-> 4 -> NULL prints 1<->2<->4<->NULL
Function Code
void printdll (){ Node ***** temp = head ; while( temp != NULL ){ cout << temp -> data << "<->" ; temp = temp -> next ; } cout << "NULL" << endl ; }
Key Points - Could also be printed backward from tail using prev pointers — a good way to demonstrate the DLL’s bidirectional capability in an interview. - Correctly uses temp != NULL (no off-by-one bug here, unlike the SLL’s search).
Alternative Approach A backward print using tail/prev is a natural extension: Node* t = tail; while(t) { cout << t->data; t = t->prev; }.
2.3 Circular Linked List
The last node’s next points back to head instead of NULL, forming a ring. This is useful for round-robin scheduling, circular buffers, and problems like the Josephus problem.
Problem Solved Adds a new node at the front of the ring, in O(1), while preserving the circular property (tail->next must still point to the — new — head).