← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
May 2026

Summary

Meta coding round for a software engineer role. One question, but it had enough moving parts to keep me busy for the whole session.

Questions Asked (1)

Q1

Given the head of a singly linked list and a target value v, find the first two nodes whose values equal v (call them A and B, A appearing before B), then reverse the sublist strictly between A and B in place. If fewer than two such nodes exist, return the list unchanged. Can you do it in one pass with O(1) extra space?

Algorithms & Data Structures
Author's notes

The core reversal wasn't the hard part.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Traverse the list once with two pointers to locate the first two nodes with value v, then reverse the sublist between them using standard pointer manipulation. Handle edge cases like adjacent nodes or fewer than two matches by returning the list unchanged.

Pro tip: Clarify whether the target nodes themselves should be reversed or only the nodes strictly between them; explicitly state your assumption and handle the edge case where A and B are adjacent (empty sublist).

1. Clarify requirements and edge cases

Confirm that only nodes strictly between A and B are reversed, and that if fewer than two nodes with value v exist, the list is unchanged. Consider cases like empty list, single node, adjacent matches, and all nodes matching.

2. Locate first two matching nodes

Traverse the list with a single pointer, counting occurrences of value v. Record the first node (A) and the second node (B) when found. If a second match is not found, return the original list.

3. Reverse the sublist between A and B

Use three pointers (prev, curr, next) to reverse the nodes strictly between A and B. Ensure A.next points to the new head of the reversed sublist and the tail of the reversed sublist points to B.

4. Reconnect and return the list

After reversal, reconnect the reversed sublist properly: A.next should point to the new first node of the reversed portion, and the last node of the reversed portion should point to B. Return the head of the modified list.

5. Analyze complexity and test

State that the solution runs in O(n) time and O(1) extra space. Walk through a few test cases (e.g., matches at head/tail, adjacent matches) to verify correctness.

Key Points to Mention

  • Single-pass traversal to find A and B while maintaining O(1) space.
  • In-place reversal of a sublist using iterative pointer manipulation.
  • Handling edge cases: fewer than two matches, adjacent matches (empty sublist), matches at head or tail.
  • Correct reconnection of pointers to avoid breaking the list.
  • Time complexity O(n) and space complexity O(1).
  • Clarifying assumptions about whether A and B are inclusive or exclusive in the reversal.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.