← Meta Interview Insights

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

SeniorPrefer not to say
Jun 2026

Summary

Went through a coding round for an MLE role at Meta. Three questions, all algorithmic, nothing ML-specific which surprised me a bit. Pretty standard faang-style session but the median of two sorted arrays one nearly broke me.

Questions Asked (3)

Q1

Given an m x n binary grid of 0s and 1s, find the size of the largest connected component of 1s using 4-directional connectivity.

Algorithms & Data Structures
Author's notes

This was the warmup and I treated it like one, which was fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Treat the grid as a graph where each '1' is a node connected to its 4-directional neighbors. Use DFS or BFS to explore each connected component, keeping track of the maximum size found. Alternatively, use Union-Find to merge adjacent '1's and track component sizes.

Pro tip: Mention that you can modify the grid in-place to mark visited cells (e.g., set to '0') to avoid extra space, but clarify that this mutates the input. Also, discuss trade-offs between DFS (recursion depth risk) and BFS (queue memory) for large grids.

1. Clarify problem and constraints

Confirm grid dimensions, connectivity definition (4-directional), and whether the grid can be modified. Ask about edge cases like empty grid or no 1s.

2. Choose an algorithm

Select DFS, BFS, or Union-Find based on constraints. For large grids, BFS avoids recursion limits; Union-Find is efficient for dynamic connectivity but may be overkill.

3. Implement traversal

Iterate through each cell; when a '1' is found, explore its component using the chosen method, counting the size. Mark visited cells to avoid revisiting.

4. Track maximum size

After each component traversal, update the global maximum size. Return the maximum after processing all cells.

5. Analyze complexity and edge cases

State time complexity O(m*n) and space complexity O(m*n) for visited tracking (or O(1) if in-place). Discuss handling of edge cases like all 0s or all 1s.

Key Points to Mention

  • Time and space complexity analysis: O(m*n) time, O(m*n) space for visited set or recursion stack.
  • Choice of traversal: DFS (recursive/iterative), BFS (queue), or Union-Find with path compression.
  • In-place modification to mark visited cells (e.g., set to '0') to save space, noting side effects.
  • Handling edge cases: empty grid, no 1s, single row/column, large grids causing stack overflow.
  • Optimization: early termination if remaining cells cannot exceed current max, or using direction arrays for neighbor checks.
  • Relevance to ML: connected components in image segmentation, graph-based clustering, or processing sparse data.

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

Q2

Given two separately sorted arrays, find the median of all elements combined without merging the arrays first. How do you do it optimally and what's the complexity?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is the one that got me.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that you can use a binary search on the smaller array to partition both arrays such that the left half contains exactly half of the total elements, and the max of the left half is ≤ the min of the right half. Then compute the median from the boundary elements. This yields O(log(min(m,n))) time and O(1) space.

Pro tip: Mention edge cases like empty arrays, one array much smaller than the other, and even/odd total lengths, and clarify that you're binary searching on the smaller array to minimize complexity.

1. Clarify the problem and constraints

Confirm that the arrays are sorted, can be of different sizes, and may be empty. State that the goal is to find the median without merging, ideally in logarithmic time.

2. Define the partition concept

Explain that you need to partition both arrays into left and right halves such that all elements in the left half are ≤ all elements in the right half, and the left half has exactly (m+n+1)/2 elements.

3. Binary search on the smaller array

Perform binary search on the smaller array to find the correct partition index. For each mid, compute the corresponding partition in the other array and check if the max of the left half ≤ min of the right half.

4. Compute the median

Once the correct partition is found, if the total number of elements is odd, the median is the maximum of the left half. If even, it's the average of the max of the left half and the min of the right half.

5. Analyze complexity and edge cases

State that time complexity is O(log(min(m,n))) and space is O(1). Discuss handling empty arrays and ensuring indices are within bounds.

Key Points to Mention

  • Binary search on the smaller array to achieve O(log(min(m,n))) time.
  • Partitioning logic: left half size = (m+n+1)/2, and condition maxLeft ≤ minRight.
  • Handling even and odd total lengths for median calculation.
  • Edge cases: empty arrays, one array much smaller, all elements in one array less than the other.
  • Constant space O(1) and no extra merging.
  • Comparison with naive merge approach O(m+n) to highlight optimization.

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

Q3

Implement BFS and DFS over a potentially disconnected undirected graph, returning the full visitation order across all vertices. Cover both recursive and iterative versions and discuss complexity.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Felt like a relief after the median question.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the graph representation (adjacency list) and the need to handle disconnected components by iterating over all vertices. Then present BFS and DFS (both iterative and recursive) with a shared visited set, and analyze time and space complexity.

Pro tip: Emphasize that for large-scale ML graphs (e.g., social networks at Meta), iterative BFS is preferred to avoid recursion depth limits and to enable distributed processing, while recursive DFS is simpler but risks stack overflow.

1. Clarify assumptions and graph representation

Confirm the graph is undirected, possibly disconnected, and represented as an adjacency list. Discuss input size and whether recursion depth is a concern.

2. Outline the general traversal strategy

Explain that you will maintain a global visited set and iterate over all vertices to ensure disconnected components are covered. For each unvisited vertex, launch a traversal.

3. Present BFS implementation

Describe the iterative BFS using a queue: enqueue the start vertex, mark visited, then process neighbors level by level. Mention that BFS naturally handles disconnected graphs via the outer loop.

4. Present DFS implementations

Show both recursive DFS (using call stack) and iterative DFS (using an explicit stack). Highlight that iterative DFS may need to push neighbors in reverse order to mimic recursion order, and that both handle disconnected graphs with the outer loop.

5. Analyze complexity and trade-offs

State that time complexity is O(V + E) for both BFS and DFS. Space complexity is O(V) for visited set and queue/stack, plus recursion depth for recursive DFS. Discuss when to choose BFS vs DFS based on memory and graph structure.

Key Points to Mention

  • Handling disconnected components by iterating over all vertices and checking the visited set.
  • Time complexity O(V + E) and space complexity O(V) for both BFS and DFS.
  • Recursive DFS uses the call stack, which may cause stack overflow for deep graphs; iterative DFS uses an explicit stack.
  • BFS uses a queue and is ideal for finding shortest paths in unweighted graphs; DFS is useful for topological sorting and cycle detection.
  • For ML applications, graph traversal is used in feature propagation, node embeddings, and sampling; iterative approaches scale better.
  • Implementation details: marking visited when enqueuing/pushing to avoid duplicates, and order of neighbor processing.

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