Skip to Content
Course content

241: Mock Coding Interview Walkthrough in C

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Alright, let's put you in the hot seat. Imagine we're in a technical interview. The interviewer looks at you and says, "I want you to write a function that reverses a singly linked list in place."

Now, if you've seen this problem before, your brain might immediately jump to the "correct" three-pointer solution. But in a real interview, the process of getting there is more important than the final code. I'm going to walk through this exactly how I would if I were thinking out loud in the room, including the mistakes I'd probably make if I rushed.

The "Wait, where did my list go?" moment

My first instinct is to just loop through and flip the next pointer of each node to point backward. Let's try to sketch that out quickly.

struct Node {
    int data;
    struct Node* next;
};

struct Node* reverseList(struct Node* head) {
    struct Node* prev = NULL;
    struct Node* current = head;

    while (current != NULL) {
        current->next = prev; // Flip the pointer
        prev = current;       // Move prev forward
        current = current->next; // Move current forward... wait.
    }
    return prev;
}

If I ran this, I'd hit a wall immediately. Look at those last two lines inside the loop. I just set current->next to prev. Then, I try to move current forward by assigning it to current->next. But current->next is now pointing backwards!

I've effectively cut the cord to the rest of my list. I'm now stuck in a loop between the first two nodes, or I'm jumping back to NULL. I've lost the reference to the rest of the chain. This is the classic "pointer trap" in C.

Keeping a finger on the next node

To fix this, I need to "bookmark" the rest of the list before I break the link. I need a third pointer. Let's call it nextTemp. I'll use it to hold onto the remainder of the list while I'm busy flipping the current node's direction.

struct Node* reverseList(struct Node* head) {
    struct Node* prev = NULL;
    struct Node* current = head;
    struct Node* nextTemp = NULL;

    while (current != NULL) {
        // 1. Save the next node (the bookmark)
        nextTemp = current->next;

        // 2. Reverse the link
        current->next = prev;

        // 3. Shift pointers forward for the next iteration
        prev = current;
        current = nextTemp;
    }
    return prev;
}

This feels much more stable. I'm saving the future (nextTemp), changing the present (current->next), and then stepping into the future. I like to visualize this as sliding three beads along a string. If I don't have that third bead, I'm just moving back and forth between two points.

Handling the edge cases

Now, before I tell the interviewer "I'm done," I need to stress-test this in my head. What happens if the list is empty? If head is NULL, current starts as NULL, the while loop never runs, and I return prev, which is NULL. That's correct.

What if there's only one node? current is the head, nextTemp becomes NULL, current->next becomes NULL (which it already was), prev becomes the head, and current becomes NULL. I return prev, which is the single node. Also correct.

The logic holds up. The key takeaway here isn't just the three-pointer trick; it's the habit of questioning your pointer assignments. In C, the moment you overwrite a pointer, that memory address is gone unless you've stored it somewhere else first. Always ask yourself: "If I change this link, do I still have a way to get to the rest of my data?"




📋 Practical Task

Exercise: Implementing a K-Group Linked List Reversal

In the lesson, we reversed an entire linked list. Now, I want you to push that logic further. Write a function reverseKGroup that reverses the nodes of a linked list k at a time and returns the modified list.

Requirements:

  • If the number of nodes is not a multiple of k, the left-over nodes at the end should remain in their original order.
  • You must perform the reversal in-place (do not create a new list).
  • Your function signature should be: struct Node* reverseKGroup(struct Node* head, int k);

Example:
Input: 1 -> 2 -> 3 -> 4 -> 5, k = 2
Output: 2 -> 1 -> 4 -> 3 -> 5

Rating
0 0

There are no comments for now.

to be the first to leave a comment.