MS

Conceptual Problems on Linked Lists, Stacks, and Queues with Big O Constraints Design solutions conceptually (no code) while adhering to specific time

Conceptual Problems on Linked Lists, Stacks, and Queues with Big O Constraints

Design solutions conceptually (no code) while adhering to specific time/space complexities.


1. Linked List: Detect Cycle

Problem: Determine if a singly linked list has a cycle (a node points to a previous node).
Constraints:

  • Time: O(n)

  • Space: O(1)

Solution Approach:

  • Use Floyd’s Tortoise and Hare algorithm:

    1. Initialize two pointers: slow (moves 1 node/step) and fast (moves 2 nodes/step).

    2. Traverse the list. If fast meets slow, a cycle exists.

    3. If fast reaches null, no cycle exists.

  • Why O(1) space? Only two pointers are used, regardless of list size.


2. Stack: Sort Elements Using One Additional Stack

Problem: Sort a stack in ascending order using only one temporary stack.
Constraints:

  • Time: O(n²)

  • Space: O(n) (for the temporary stack)

Solution Approach:

  1. Use the primary stack for input and the temporary stack for sorted output.

  2. While the primary stack is not empty:

    • Pop the top element temp from the primary stack.

    • Move all elements greater than temp from the temporary stack back to the primary stack.

    • Push temp into the temporary stack.

  3. The temporary stack will hold sorted elements.

  • Why O(n²) time? Each element may require multiple transfers between stacks.


3. Queue: Reverse Using a Stack

Problem: Reverse the order of elements in a queue.
Constraints:

  • Time: O(n)

  • Space: O(n)

Solution Approach:

  1. Dequeue all elements from the queue and push them onto a stack.

  2. Pop elements from the stack and enqueue them back into the original queue.

  • Result: The queue is reversed.

  • Why O(n) space? The stack temporarily stores all n elements.


4. Linked List: Find Middle Element in One Pass

Problem: Identify the middle node of a singly linked list without knowing its length.
Constraints:

  • Time: O(n)

  • Space: O(1)

Solution Approach:

  • Use the two-pointer technique:

    1. Initialize slow and fast pointers at the head.

    2. Move slow by 1 node and fast by 2 nodes per step.

    3. When fast reaches the end, slow will be at the middle.

  • Why O(n) time? The list is traversed once.


5. Stack: Check Balanced Parentheses

Problem: Determine if a string of parentheses (){}[] is balanced.
Constraints:

  • Time: O(n)

  • Space: O(n)

Solution Approach:

  1. Use a stack to track opening brackets.

  2. For each character in the string:

    • If it’s an opening bracket, push it onto the stack.

    • If it’s a closing bracket, check if it matches the top of the stack.

      • If yes, pop the stack.

      • If no, return false.

  3. The string is balanced if the stack is empty at the end.

  • Why O(n) space? In the worst case (all opening brackets), the stack holds n elements.


6. Queue: Implement Using Two Stacks

Problem: Design a queue where enqueue and dequeue operations meet amortized O(1) time.
Constraints:

  • Time: O(1) amortized for both operations.

  • Space: O(n)

Solution Approach:

  • Use two stacks: stack1 for enqueue, stack2 for dequeue.

  1. Enqueue: Push elements into stack1 (O(1)).

  2. Dequeue:

    • If stack2 is empty, transfer all elements from stack1 to stack2 (reversing order).

    • Pop from stack2 (O(1) amortized, as each element is moved once).


Key Takeaways:

  • Linked Lists: Two-pointer techniques optimize traversal and detection.

  • Stacks/Queues: Use auxiliary stacks/queues to rearrange elements efficiently.

  • Space Constraints: Often trade time for space (e.g., stacks for reversing queues).

Thought for 14 seconds

Additional Conceptual Problems on Linked Lists, Stacks, and Queues

Design solutions conceptually (no code) while adhering to specific time/space complexities.


7. Linked List: Remove Nth Node from End

Problem: Remove the Nth node from the end of a singly linked list in one pass.
Constraints:

  • Time: O(n)

  • Space: O(1)

Solution Approach:

  1. Use two pointers: fast and slow.

  2. Move fast N nodes ahead of slow.

  3. Traverse the list until fast reaches the end.

  4. The slow pointer will be at the node just before the target. Remove the target node.

  • Key Insight: The gap between fast and slow ensures slow stops at the correct position.


8. Stack: Implement a Min Stack

Problem: Design a stack that supports push, pop, and getMin (retrieve the minimum element) in O(1) time for all operations.
Constraints:

  • Time: O(1) per operation.

  • Space: O(n).

Solution Approach:

  1. Use two stacks:

    • Main stack for elements.

    • Auxiliary stack to track the current minimum.

  2. Push:

    • Add the element to the main stack.

    • If the element ≤ current minimum (from auxiliary stack), push it to the auxiliary stack.

  3. Pop:

    • Remove from the main stack.

    • If the popped element equals the top of the auxiliary stack, pop the auxiliary stack.

  4. getMin: Return the top of the auxiliary stack.


9. Queue: Generate Binary Numbers

Problem: Generate binary numbers from 1 to N using a queue.
Constraints:

  • Time: O(n)

  • Space: O(n)

Solution Approach:

  1. Initialize the queue with "1".

  2. For each iteration:

    • Dequeue the front element.

    • Append "0" and "1" to create the next two binary numbers.

    • Enqueue these new numbers.

  3. Repeat until N numbers are generated.

  • Example:

    • Queue starts with "1".

    • For N=3: "1" → "10", "11" → "100", "101", "110", "111".


10. Linked List: Check Palindrome

Problem: Determine if a singly linked list is a palindrome.
Constraints:

  • Time: O(n)

  • Space: O(1)

Solution Approach:

  1. Use two pointers to find the middle of the list.

  2. Reverse the second half of the list.

  3. Compare the first half with the reversed second half.

  4. Restore the reversed half (optional).

  • Why O(1) space? Reversing in-place avoids using extra memory.


11. Stack: Evaluate Postfix Expression

Problem: Evaluate a postfix expression (e.g., 3 4 + 2 * = 14) using a stack.
Constraints:

  • Time: O(n)

  • Space: O(n)

Solution Approach:

  1. Traverse the expression left to right.

  2. For each token:

    • If it’s a number, push to the stack.

    • If it’s an operator, pop two operands, compute the result, and push the result back.

  3. The final value in the stack is the result.

  • Example:

    • 3 4 + 2 → Stack: [3] → [3,4] → pop 3,4 → 3+4=7 → push 7 → push 2 → pop 7,2 → 72=14.


12. Queue: Reverse First K Elements

Problem: Reverse the order of the first K elements in a queue.
Constraints:

  • Time: O(n)

  • Space: O(K) (use a stack for reversal).

Solution Approach:

  1. Dequeue the first K elements and push them onto a stack.

  2. Pop elements from the stack and enqueue them back (reversing the first K).

  3. Dequeue the remaining n-K elements and enqueue them back in original order.

  • Example:

    • Queue: [1,2,3,4,5], K=3 → Stack: [1,2,3] → Reversed: [3,2,1] → Queue becomes [3,2,1,4,5].


13. Linked List: Flatten a Multilevel List

Problem: Flatten a multilevel linked list (nodes may have child pointers) into a single-level list.
Constraints:

  • Time: O(n)

  • Space: O(1)

Solution Approach:

  1. Traverse the list. When a node with a child is found:

    • Merge the child list into the main list.

    • Adjust pointers to skip the child hierarchy.

  2. Use depth-first traversal to prioritize child nodes.

  • Visualization:

    • Input: 1 → 2 → 3 → 4

      5 → 6

    • Output: 1 → 2 → 5 → 6 → 3 → 4.


14. Stack: Design a Browser History

Problem: Simulate browser history (back/forward navigation) using two stacks.
Constraints:

  • Time: O(1) for visit, back, and forward operations.

  • Space: O(n).

Solution Approach:

  1. Main Stack: Stores visited pages in chronological order.

  2. Forward Stack: Temporarily stores pages when moving back.

  3. Visit: Clear the forward stack, push the new page to the main stack.

  4. Back: Pop from the main stack and push to the forward stack.

  5. Forward: Pop from the forward stack and push to the main stack.


Key Takeaways:

  • Linked Lists: Leverage pointer manipulation and traversal techniques.

  • Stacks/Queues: Use auxiliary structures to rearrange elements efficiently.

  • Time vs. Space Trade-offs: Optimize for constraints (e.g., reversing queues with stacks).

Thought for 24 seconds

More Conceptual Problems on Linked Lists, Stacks, and Queues

Design solutions conceptually (no code) while adhering to specific time/space complexities.


15. Linked List: Merge Two Sorted Lists

Problem: Merge two sorted singly linked lists into one sorted list.
Constraints:

  • Time: O(n + m) (where nn and mm are the lengths of the two lists).

  • Space: O(1) (reuse existing nodes, no new data structures).

Solution Approach:

  1. Use a dummy node to simplify edge cases (e.g., empty lists).

  2. Compare nodes from both lists, appending the smaller value to the merged list.

  3. Advance the pointer of the list from which the node was taken.

  4. Continue until both lists are exhausted.

  • Key Insight: Reassign pointers in-place to avoid extra memory.


16. Stack: Validate Stack Sequences

Problem: Given two sequences pushed and popped, determine if they represent valid operations on an initially empty stack.
Constraints:

  • Time: O(n)

  • Space: O(n) (for simulating the stack).

Solution Approach:

  1. Simulate the stack:

    • Push elements from pushed in order.

    • Whenever the top of the stack matches the current popped element, pop it.

  2. If the stack is empty after processing all elements, the sequences are valid.

  • Example:

    • pushed = [1,2,3,4,5], popped = [4,5,3,2,1] → Valid.

    • popped = [4,3,5,1,2] → Invalid (1 cannot be popped before 2).


17. Queue: Circular Buffer Implementation

Problem: Design a circular queue with a fixed size KK that supports enqueue, dequeue, and isEmpty operations.
Constraints:

  • Time: O(1) for all operations.

  • Space: O(K) (fixed-size array).

Solution Approach:

  1. Use an array of size KK and two pointers: front (points to the first element) and rear (points to the next empty slot).

  2. Enqueue:

    • Check if the queue is full using (rear + 1) % K == front.

    • Insert at rear and update rear = (rear + 1) % K.

  3. Dequeue:

    • Remove the element at front and update front = (front + 1) % K.

  • Edge Case: Handle wrap-around using modulo arithmetic.


18. Linked List: Remove Duplicates from Sorted List

Problem: Remove all duplicates from a sorted singly linked list, leaving only distinct values.
Constraints:

  • Time: O(n)

  • Space: O(1)

Solution Approach:

  1. Traverse the list with a pointer current.

  2. For each node, check if its value equals the next node’s value.

  3. If duplicates exist, skip all duplicate nodes by adjusting pointers.

  • Example:

    • Input: 1 → 1 → 2 → 3 → 3

    • Output: 1 → 2 → 3


19. Stack: Largest Rectangle in Histogram

Problem: Given an array of heights representing a histogram, find the area of the largest rectangle.
Constraints:

  • Time: O(n)

  • Space: O(n) (for tracking indices and heights).

Solution Approach:

  1. Use a stack to track indices of bars in increasing order of height.

  2. For each bar:

    • Pop from the stack while the current bar’s height is less than the stack’s top.

    • Calculate the area using the popped index as the height and the distance between the current index and the new stack top as the width.

  3. Update the maximum area during each calculation.

  • Key Insight: The stack maintains the "last smaller bar" for efficient area computation.


20. Queue: Interleave First and Second Halves

Problem: Interleave the first half of a queue with the second half.
Constraints:

  • Time: O(n)

  • Space: O(n) (use a stack/queue for temporary storage).

Solution Approach:

  1. Split the queue into two halves using a stack or temporary queue.

  2. Re-insert elements by alternating between the first and second halves.

  • Example:

    • Input: [1,2,3,4,5,6] → Split into [1,2,3] and [4,5,6].

    • Output: [1,4,2,5,3,6].


21. Linked List: Rotate List by K Places

Problem: Rotate a linked list to the right by KK positions.
Constraints:

  • Time: O(n)

  • Space: O(1)

Solution Approach:

  1. Compute the effective rotation Keff=K%nKeff​=K%n (avoids redundant rotations).

  2. Use two pointers to find the new tail and head:

    • Traverse to the (n−Keff)(nKeff​)th node.

    • Set this node as the new tail and link the original tail to the original head.

  • Example:

    • Input: 1 → 2 → 3 → 4 → 5, K=2K=2 → Output: 4 → 5 → 1 → 2 → 3.


22. Stack: Simplify File Path

Problem: Convert an absolute Unix-style file path (e.g., /a/./b/../c/) to its canonical form.
Constraints:

  • Time: O(n)

  • Space: O(n) (split path into components).

Solution Approach:

  1. Split the path by / and process each component:

    • Ignore . and empty strings.

    • Pop from the stack on ...

    • Push valid directory names onto the stack.

  2. Join the stack to form the canonical path.

  • Example:

    • /a/./b/../c/ → Stack: ["a", "c"] → Canonical path: /a/c.


23. Queue: Design a Recent Calls Counter

Problem: Implement a RecentCounter that counts the number of requests in the last 3000 milliseconds.
Constraints:

  • Time: O(1) amortized for ping().

  • Space: O(n) (store timestamps in a queue).

Solution Approach:

  1. Use a queue to store timestamps.

  2. For each new ping(t), remove all timestamps older than t−3000t−3000 from the front of the queue.

  3. Add tt to the queue and return its length.

  • Example:

    • ping(1) → 1, ping(3002) → 2 (since 3002 - 3000 = 2 > 1).


Key Takeaways:

  • Linked Lists: Focus on pointer manipulation and traversal patterns (e.g., two-pointer, in-place reversal).

  • Stacks: Leverage LIFO order for problems involving nested structures or backtracking (e.g., parentheses, histograms).

  • Queues: Use FIFO order for sequence-based operations (e.g., interleaving, sliding windows).

  • Space-Time Trade-offs: Optimize for constraints (e.g., circular queues for fixed-space efficiency).