# Python Interview with a Meta engineer

#### Watch someone solve the remove nth node from end of list problem in an interview with a Meta engineer and see the feedback their interviewer left them. Explore this problem and others in our library of interview replays.

### Interview Summary

**Problem type**  
Remove Nth Node From End of List

**Interview question**  
Remove the Nth Node from the end of the list

### Interview Feedback

**Feedback about Massively Parallel Nougat (the interviewee)**

Advance this person to the next round?  
Yes

**How were their technical skills?**  
4/4

**How was their problem solving ability?**  
3/4

**What about their communication ability?**  
4/4

> Positives:  
> 1. Excellent articulation of thought process with the aid of examples. You kept the interviewer engaged and following throughout solutioning.  
> 2. Almost exhaustive list of clarifying questions (missed just one about double-link list)  
> 3. Produced functional code, and quick to amend it as we discovered edge cases  
> 4. Proactively dry-ran the code with good set of edge cases to identify bugs.  
> 5. Responsive to hints and quick to implement them (pre-empt parsing the entire list if K =0)  
> 6. Discussed multiple approaches and compared tradeoffs of them with correct complexity analysis.
>  
> Can do better:  
> 1. Don’t miss any clarifying questions or assumptions  
> 2. Try to keep time for complex followups if interviewing for a senior role. This is particularly applicable if the asked question is a simple one. You can also clarify with the interviewer if there might be followups after the initial question is mentioned to manage time better. However, best is to finish the solution as fast as possible without compromising on quality.  
> 3. Improve problem solving with distributed workers (very large datasets).

**Feedback about Laser Tardigrade (the interviewer)**

Would you want to work with this person?  
Yes

**How excited would you be to work with them?**  
4/4

**How good were the questions?**  
4/4

**How helpful was your interviewer in guiding you to the solution(s)?**  
4/4

> Great interview. Appreciate the guided approach to explore problem space, discuss solution, then to code. Also great follow-up discussion.

### Interview Transcript

**Massively Parallel Nougat:** Hello?  
**Laser Tardigrade:** Hi, can you hear me?  
**Massively Parallel Nougat:** Yes, I can hear you. Hello can you hear me?  
**Laser Tardigrade:** All right here, I can hear you. Let's get started  
**Massively Parallel Nougat:** Right sounds good.  
**Laser Tardigrade:** Okay, so we'll jump right into the question. We are going to have an interview on coding and problem solving, right?  
**Massively Parallel Nougat:** Yes.  
**Laser Tardigrade:** Yeah, that's the expectation. Okay, so I'm gonna ask you a very simple question and I would encourage you to ask me follow-up questions to disambiguate the problem and also lay out any assumptions that you're making. Don’t jump to problem solving directly or writing the code. I would encourage you to also discuss the problem with me, the approach, how you're thinking about the problem. And eventually once we agree, we can start thinking about the code and then writing the code as part of this interview. I would expect you to disambiguate. Like do all these things, discuss these things with me and then eventually also produce the working code for the problem that I want to ask you.  
**Massively Parallel Nougat:** Great, that sounds good.

---

### Problem Understanding

**Laser Tardigrade:** The problem I wanted to ask you was suppose that you have a linked list and you need to delete a specific node from the back of the list. Right? So suppose you have a linked list containing 10 nodes and you will be given this linked list and you would also be given something like you need to delete the kth node from the back which may be five or which may be something like three or which may be the very last node. So this is what you need to do; have the node read from the end of the linked list and then return the modified linked list.

**Massively Parallel Nougat:** Gotcha, okay, sounds good. Can I just write an example here? So if k was like k equal to one then we want to return the list like that right?  
**Laser Tardigrade:** Yep  
**Massively Parallel Nougat:** Are there cycles in the list?  
**Laser Tardigrade:** Um, there  
**Massively Parallel Nougat:** I guess I wouldn't make sense because then it couldn't be K from the end, right?  
**Laser Tardigrade:** Exactly. That's a very good question. Okay, let's assume that there are no cycles on the list.  
**Massively Parallel Nougat:** Okay no cycles.  
**Laser Tardigrade:** And will k always be greater than or less than the length of the list?  
**Massively Parallel Nougat:** It might be but it could also be greater than.

### Thought Process

**Laser Tardigrade:** Let’s see here, can the head node be null?  
**Massively Parallel Nougat:** Yep  
**Laser Tardigrade:** Like the pointer. Okay, okay um let’s see is there anything else here? Yeah, I don’t know I don’t think there’s any other questions I have, I think that’s pretty quite well.

---

### Solution Approaches

#### Approach 1: Two Pass Solution
1. Iterate over the linked list to calculate its length.
2. Compute the Position to Remove = Length - k.
3. Iterate again to the (Position - 1) to remove the node.

#### Approach 2: One Pass Solution
1. Use two pointers; first pointer moves k nodes ahead.
2. Move both pointers until the first pointer reaches the end. Now, the second pointer will be at the node before the target node.
3. Adjust pointers to remove the target node.

### Code Implementation

```python
class ListNode:
    def __init__(self, value=0, next=None):
        self.value = value
        self.next = next

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        dummy = ListNode(0, head)
        first = second = dummy
        for _ in range(n + 1):
            first = first.next
        while first:
            first = first.next
            second = second.next
        second.next = second.next.next  # Remove target node
        return dummy.next
```

### Complexity Analysis
- **Time Complexity:** O(N) where N is the number of nodes in the linked list.
- **Space Complexity:** O(1) since we are using constant space.
