Bird
Raised Fist0
Interview Prepfast-slow-pointerseasyAmazonBloomberg

Nth Node from End of List (Return Value)

Choose your preparation mode4 modes available
▶
Steps
setup

Initialize pointers and stack

We start with the head of the list pointing to the first node (value 10). The stack is empty, and the current pointer is set to head to begin traversal.

💡 Setting up the current pointer and empty stack is essential to start traversing the list and collecting nodes.
Line:stack = [] current = head
💡 The current pointer will move through the list to push all nodes onto the stack.
📊
Nth Node from End of List (Return Value) - Watch the Algorithm Execute, Step by Step
Watching each push and pop operation helps you understand how reversing traversal with a stack works to find the nth node from the end.
Step 1/10
·Active fill★Answer cell
advance
10
→
20
→
30
→
40
→
50
connect
10
→
20
→
30
→
40
→
50
connect
10
→
20
→
30
→
40
→
50
connect
10
→
20
→
30
→
40
→
50
connect
10
→
20
→
30
→
40
→
50
advance
10
→
20
→
30
→
40
→
50
compare
10
→
20
→
30
→
40
→
50
detach
10
→
20
→
30
→
40
→
50
detach
10
→
20
→
30
→
40
→
50
Result: 40
reconstruct
10
→
20
→
30
→
40
→
50
Result: 40

Key Takeaways

✓ Using a stack reverses the traversal order, allowing easy access to nodes from the end.

This insight is hard to see from code alone because the stack abstracts the reversal, but visualization shows nodes being collected and popped in reverse.

✓ Popping n nodes from the stack corresponds to moving backward n steps from the list's end.

Visualizing each pop clarifies how the algorithm counts backward without modifying the list.

✓ The check for n > stack size prevents errors when n is larger than the list length.

Seeing this decision step helps understand the importance of input validation in linked list problems.

Practice

(1/5)
1. You are given a singly linked list and need to determine the length of a cycle if one exists. Which approach guarantees an optimal solution with O(n) time and O(1) space complexity?
easy
A. Use dynamic programming to store cycle lengths for sublists and combine results.
B. Use a hash set to store visited nodes and count cycle length upon revisiting a node.
C. Use two pointers moving at different speeds to detect the cycle and then count its length by traversing the cycle once.
D. Use a recursive depth-first search to detect cycles and count their lengths.

Solution

  1. Step 1: Understand the problem constraints

    The problem requires detecting a cycle and counting its length efficiently in a linked list.
  2. Step 2: Evaluate approaches for time and space complexity

    Using two pointers (fast and slow) allows cycle detection in O(n) time and O(1) space, then counting the cycle length by traversing the cycle once is optimal.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Fast-slow pointer approach is classic for cycle detection with constant space [OK]
Hint: Fast-slow pointers detect cycles in O(1) space [OK]
Common Mistakes:
  • Believing hash sets are optimal due to extra space
  • Confusing DP with cycle detection
  • Using recursion unnecessarily
2. You are given a singly linked list and need to find the node that is exactly in the middle of the list. Which approach guarantees finding the middle node in a single pass with constant extra space?
easy
A. Store all nodes in an array, then access the middle index directly.
B. Traverse the list twice: first to count nodes, second to reach the middle node.
C. Use two pointers: move one pointer twice as fast as the other; when the fast pointer reaches the end, the slow pointer is at the middle.
D. Use a recursive approach to reach the end and count backwards to the middle.

Solution

  1. Step 1: Understand the problem constraints

    The goal is to find the middle node in a single pass and O(1) space.
  2. Step 2: Identify the approach that uses two pointers

    Using a slow pointer moving one step and a fast pointer moving two steps ensures when fast reaches the end, slow is at the middle.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Two-pointer technique is classic for single-pass middle node [OK]
Hint: Two pointers with different speeds find middle in one pass [OK]
Common Mistakes:
  • Thinking counting then traversing is single pass
  • Using extra space unnecessarily
  • Recursion adds overhead and is not optimal
3. Consider the following buggy code snippet for detecting a circular array loop. Which line contains the subtle bug that causes incorrect detection of single-element loops as valid cycles?
medium
A. Line with 'if nums[i] == 0: continue' - skipping zeros prematurely
B. Line with 'if slow == fast: return True' - missing check for single-element loop
C. Line with 'direction = nums[i] > 0' - direction assignment incorrect
D. Line with 'nums[slow] = 0' - zeroing visited elements too early

Solution

  1. Step 1: Identify where single-element loops are checked

    The original code breaks if slow == next_index(slow) to avoid single-element loops.
  2. Step 2: Locate missing check

    The buggy code returns True immediately when slow == fast without verifying cycle length.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Missing single-element loop check causes false positives [OK]
Hint: Check cycle length before returning True to avoid single-element loops [OK]
Common Mistakes:
  • Returning True immediately on pointer meet
  • Ignoring direction consistency
  • Incorrectly zeroing elements
4. Identify the bug in the following Python code for checking if a number is happy. The code attempts to detect cycles using recursion but lacks proper cycle detection.
def isHappy(n: int) -> bool:
    def get_next(number):
        total_sum = 0
        while number > 0:
            digit = number % 10
            total_sum += digit * digit
            number //= 10
        return total_sum

    def helper(num):
        if num == 1:
            return true
        return helper(get_next(num))

    return helper(n)
medium
A. Missing base case for cycle detection causing infinite recursion
B. Incorrect sum of squares calculation in get_next function
C. Returning helper(get_next(num)) without checking if num is 1
D. Not initializing total_sum to zero before summing digits

Solution

  1. Step 1: Analyze recursion base cases

    The code only stops recursion if num == 1. It does not detect cycles, so for unhappy numbers it recurses infinitely.
  2. Step 2: Identify missing cycle detection

    Without tracking visited numbers or using fast-slow pointers, the recursion never terminates for cycles, causing stack overflow.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Infinite recursion due to missing cycle detection base case [OK]
Hint: Check for cycle detection in recursion to avoid infinite loops [OK]
Common Mistakes:
  • Assuming recursion stops at cycles without explicit detection
5. Consider the following buggy code for finding the middle node of a linked list. Which line contains the subtle bug that can cause a runtime error?
medium
A. Line 4: while fast.next and fast.next.next:
B. Line 3: fast = head
C. Line 2: slow = head
D. Line 6: return slow

Solution

  1. Step 1: Analyze loop condition

    The condition checks fast.next and fast.next.next but does not check if fast itself is null, which can cause AttributeError if fast is null.
  2. Step 2: Identify fix

    Change condition to while fast and fast.next to safely access fast.next.next.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Missing fast null check causes runtime error on short lists [OK]
Hint: Always check fast pointer is not null before accessing next [OK]
Common Mistakes:
  • Assuming fast.next is safe without checking fast
  • Returning first middle node incorrectly
  • Modifying list nodes accidentally
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard