← Meta Interview Insights

Meta·Machine Learning Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
Apr 2026

Summary

Did a coding round for an MLE role at Meta, four problems back to back. The first two were classic CS fundamentals but the last one had a constraint that made me rethink my whole approach mid-solve. Mixed feelings about how it went.

Questions Asked (4)

Q1

Given the head of a singly linked list that may contain a cycle, return the node where the cycle begins, or null if there is no cycle. You must use O(1) extra space.

Algorithms & Data Structures
Author's notes

Floyd's algorithm, fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use Floyd's cycle-finding algorithm (tortoise and hare) to detect a cycle and find the meeting point. Then, reset one pointer to the head and move both pointers one step at a time until they meet again; that node is the start of the cycle. If no cycle is detected, return null.

Pro tip: Clearly explain why the algorithm works, especially the mathematical proof that the distance from the head to the cycle start equals the distance from the meeting point to the cycle start (modulo the cycle length). This demonstrates deep understanding and is often expected at Meta.

1. Detect Cycle

Initialize two pointers, slow and fast, at the head. Move slow one step and fast two steps at a time until they meet or fast reaches null. If fast reaches null, there is no cycle; return null.

2. Find Meeting Point

If a cycle exists, the slow and fast pointers will meet at some node inside the cycle. Record this meeting node.

3. Find Cycle Start

Reset one pointer to the head while keeping the other at the meeting point. Move both pointers one step at a time until they meet again. The node where they meet is the start of the cycle.

4. Return Result

Return the node where the cycle begins, or null if no cycle was detected.

Key Points to Mention

  • Floyd's cycle-finding algorithm (tortoise and hare) for O(1) space and O(n) time.
  • Proof of why resetting one pointer to head and moving both at same speed finds the cycle start.
  • Handling edge cases: empty list, single node, cycle at head, no cycle.
  • Time complexity: O(n) and space complexity: O(1).
  • Alternative approaches like hash set (O(n) space) and why they are not optimal.
  • Potential follow-up: how to handle if the list is very large and cannot be modified.

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

Q2

In a BST where each node has left, right, and parent pointers, return the in-order successor of a given node. Return null if none exists.

Algorithms & Data Structures
Author's notes

Two cases: node has a right subtree, or it doesn't.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the BST is not necessarily balanced and that parent pointers are available. Then describe the two-case algorithm: if the node has a right subtree, return the leftmost node in that subtree; otherwise, walk up parent pointers until you find an ancestor for which the node lies in its left subtree. Analyze time and space complexity, noting O(h) time and O(1) space.

Pro tip: Explicitly state that the algorithm uses O(1) extra space and runs in O(h) time, where h is the tree height, and mention that this is optimal for the given structure. Also, briefly note that if the tree were balanced, h = O(log n), but in the worst case (skewed tree) it's O(n).

1. Clarify assumptions and edge cases

Confirm that the BST may be unbalanced and that parent pointers are valid. Discuss edge cases: node is null, node has no successor (e.g., maximum node), and tree with a single node.

2. Handle the right subtree case

If the given node has a right child, the in-order successor is the leftmost node in its right subtree. Explain how to find it by traversing left pointers until null.

3. Handle the no-right-subtree case

If there is no right child, walk up using parent pointers. While the current node is a right child of its parent, keep moving up. The successor is the first ancestor for which the node is in the left subtree, or null if none exists.

4. Analyze complexity and test

State that time complexity is O(h) and space is O(1). Walk through a small example to verify correctness, including cases where the successor is an ancestor or does not exist.

Key Points to Mention

  • Definition of in-order successor: the next node in the in-order traversal of the BST.
  • Two distinct cases: node has a right subtree vs. node does not have a right subtree.
  • Use of parent pointers to traverse upward without additional data structures.
  • Time complexity O(h) where h is the height of the tree; space complexity O(1).
  • Edge cases: node is the maximum element (return null), node is null, tree is skewed.
  • Comparison with alternative approaches (e.g., in-order traversal using a stack) to highlight efficiency.

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

Q3

Design a class for an n×n tic-tac-toe board. It should support a move operation that places a player's mark and returns whether that player has won. Each move should run in close to O(1) time.

Algorithms & Data StructuresSystem Design
Author's notes

I started with the naive 'scan the row and column after each move' approach and they immediately asked about the time complexity.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: n×n board, move operation that places a mark and returns whether that player wins, O(1) time per move. Then design a class that maintains row, column, and diagonal counts for each player, updating them on each move and checking for a win in constant time. Finally, discuss edge cases and potential optimizations.

Pro tip: Mention that the O(1) win check is achieved by tracking counts per row, column, and diagonal for each player, and that this approach scales to any n. Also, note that you can avoid storing the entire board if you only need to detect wins, but storing it may be useful for other operations like undo or display.

1. Clarify requirements and constraints

Confirm the board size n, the move operation signature, and that each move must be O(1). Ask about win conditions (e.g., only the player who just moved can win) and whether the board needs to be stored.

2. Design the data structures

Use arrays to track counts for each player: rowCounts[player][row], colCounts[player][col], and two variables for diagonals. Optionally, store the board as a 2D array for completeness.

3. Implement the move operation

On move(row, col, player), update the corresponding row, column, and diagonal counts. Check if any count reaches n; if so, return true, else false. Ensure O(1) time by only updating relevant counters.

4. Handle edge cases and discuss trade-offs

Consider invalid moves (out of bounds, occupied cell), multiple wins, and whether to support undo. Discuss space-time trade-offs: O(n) space for counts vs O(n^2) for board.

Key Points to Mention

  • Use separate counters for each player to avoid interference.
  • Update row, column, and diagonal counts in constant time.
  • Check win condition by comparing counts to n.
  • Handle both main diagonal (row == col) and anti-diagonal (row + col == n-1).
  • Consider storing the board for validation or display, but it's not needed for win detection.
  • Discuss potential follow-ups: undo move, multiple players, or larger boards.

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

Q4

Given an n×n binary grid where 1s are land and 0s are water, you can flip at most one 0 to a 1. Return the maximum possible island size after the flip. The grid can be up to 500x500, so near-quadratic extra work may time out.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This one hurt.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Label each island with a unique ID and compute its size using BFS/DFS, then for each water cell, sum the sizes of adjacent distinct islands plus one. The maximum of these sums is the answer, and if no water exists, return the largest island size.

Pro tip: Emphasize that the solution must be O(n^2) time and space to handle 500x500 grids, and mention that using a hash set to deduplicate adjacent island IDs avoids double-counting.

1. Clarify constraints and edge cases

Confirm grid size up to 500x500, binary values, and that flipping is optional. Discuss edge cases: no water, all water, and multiple islands.

2. Label islands and compute sizes

Traverse the grid with BFS/DFS to assign each land cell an island ID and record the size of each island in a map or array.

3. Evaluate each water cell

For each 0, collect the unique IDs of adjacent islands, sum their sizes, and add 1 for the flipped cell. Track the maximum.

4. Handle no-water case

If no water cells exist, return the size of the largest island (or 0 if grid is all water).

5. Analyze complexity and trade-offs

Explain that the algorithm runs in O(n^2) time and uses O(n^2) space for the ID grid and size map, which is optimal for this problem.

Key Points to Mention

  • Use BFS/DFS to label islands and compute sizes in a single pass.
  • For each water cell, deduplicate adjacent island IDs using a set to avoid double-counting.
  • The answer is the maximum of (sum of adjacent island sizes + 1) over all water cells.
  • If no water cells exist, return the maximum island size (or 0 if all water).
  • Time and space complexity are both O(n^2), which is necessary for n=500.
  • Avoid recomputing island sizes for each water cell; precompute and reuse.

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