← Meta Interview Insights

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

IntermediatePrefer not to say
May 2026

Summary

Onsite coding loop at Meta for a software engineer role. Three back-to-back problems covering array manipulation, graph search on a grid, and a social graph design question. A lot of ground to cover and the follow-ups kept coming.

Questions Asked (3)

Q1

Given a sorted integer array, modify it in-place so each distinct value appears exactly once at the front, preserving order. Return the prefix length. O(1) space, O(n) time. Follow-up: what if each value could appear up to k times?

Algorithms & Data Structures
Author's notes

Two-pointer setup, pretty classic.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pointer technique: one pointer (write) tracks the position for the next distinct element, and the other (read) scans the array. Since the array is sorted, compare the current element with the last written element; if different, write it at the write pointer and increment. Return the write pointer as the new length. For the follow-up, allow up to k duplicates by counting occurrences and writing only if count < k.

Pro tip: Clarify edge cases upfront (empty array, k=0) and mention that the algorithm is optimal because it makes a single pass and uses O(1) extra space. For the follow-up, emphasize that the same two-pointer approach generalizes naturally by tracking a count.

1. Clarify requirements and edge cases

Confirm the problem: in-place modification, return new length, O(1) space, O(n) time. Ask about empty arrays, k=0, and whether elements beyond the new length matter.

2. Explain the two-pointer approach

Describe using a write pointer to place the next unique element and a read pointer to scan. Since the array is sorted, duplicates are adjacent, so compare with the last written element.

3. Walk through an example

Trace the algorithm on a small array (e.g., [1,1,2,3,3]) to demonstrate how the write pointer advances and the array is modified in-place.

4. Analyze complexity

State that time is O(n) because each element is read once, and space is O(1) because only two pointers are used.

5. Address the follow-up (k duplicates)

Explain that the same two-pointer technique works by allowing up to k copies: write an element if it's different from the last written or if its count so far is less than k. Maintain a count for the current element.

Key Points to Mention

  • Two-pointer technique (read and write pointers)
  • In-place modification and returning the new length
  • Leveraging sorted order to detect duplicates by comparing with the last written element
  • Time complexity O(n) and space complexity O(1)
  • Generalization to at most k duplicates by tracking a count
  • Edge cases: empty array, all duplicates, k=0, k >= array length

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

Q2

Find the length of the longest strictly increasing path in an integer grid, where moves are up/down/left/right. How would you reconstruct the actual path, and how do you handle recursion depth on very large inputs?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

DFS with memoization is the standard move here and I got there.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by framing the problem as finding the longest path in a directed acyclic graph (DAG) where edges go from smaller to larger values. Use DFS with memoization to compute the longest path from each cell, and store parent pointers to reconstruct the path. For large inputs, replace recursion with an iterative topological sort (Kahn's algorithm) to avoid stack overflow.

Pro tip: Mention that you can optimize memory by using a 2D array for memoization and a separate 2D array for parent pointers, but if memory is tight, you can recompute the path by following the decreasing values from the end. Also, highlight that the problem is equivalent to finding the longest path in a DAG, which can be solved in O(mn) time.

1. Clarify and Define

Confirm that the path must be strictly increasing, moves are 4-directional, and the grid can be large. Discuss edge cases like empty grid, single cell, or all equal values.

2. Model as DAG

Explain that each cell is a node, and directed edges go from a cell to its neighbors with strictly larger values. This forms a DAG, so we can find the longest path efficiently.

3. Compute Longest Length

Use DFS with memoization: for each cell, recursively compute the longest path starting there, caching results. Alternatively, use topological sort (Kahn's algorithm) iteratively to avoid recursion depth issues.

4. Reconstruct Path

During computation, store the next cell (parent) for each cell that gives the maximum length. After finding the global maximum, follow the parent pointers to reconstruct the path.

5. Handle Large Inputs

For very large grids, recursion depth may exceed the stack limit. Switch to an iterative approach: compute in-degrees, perform topological sort, and update longest paths in order. This avoids recursion and handles up to millions of cells.

Key Points to Mention

  • Time and space complexity: O(mn) time and O(mn) space for memoization and parent pointers.
  • DFS with memoization vs. iterative topological sort: trade-offs in simplicity and stack safety.
  • Path reconstruction using parent pointers or by backtracking from the end using value comparisons.
  • Handling recursion depth: use iterative DFS or increase recursion limit (but not recommended for production).
  • Optimization: early termination if the maximum possible length is found, or using union-find? (No, union-find not suitable for longest path).
  • Edge cases: empty grid, single row/column, all increasing/decreasing, duplicates.

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

Q3

Design a friend recommender for a social graph. Implement getCandidates (friends-of-friends excluding direct friends), recommendRandom (uniform random pick), and recommendTopK (ranked by mutual friend count, ties broken by user ID). Then discuss how you'd extend the scoring in production.

System DesignAlgorithms & Data StructuresTechnical Trade-offs
Author's notes

This one was the most interesting of the three.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem constraints (graph size, read/write patterns, latency requirements) and then walk through the data structures and algorithms for each method. Implement getCandidates using a hash set to track friends and a queue/BFS to collect friends-of-friends, then recommendRandom by sampling from the candidate set, and recommendTopK by counting mutual friends and sorting with a tie-breaker on user ID. Finally, discuss production extensions like weighted scoring, scalability, and real-time updates.

Pro tip: Mention that you would precompute and cache friend-of-friend candidates or use approximate algorithms for large-scale graphs, and emphasize the trade-off between freshness and performance.

1. Clarify requirements and constraints

Ask about graph size, expected latency, read/write ratio, and whether recommendations need to be real-time or can be batch-computed. This shows you think about system design before coding.

2. Design data structures and getCandidates

Represent the social graph as an adjacency list (e.g., Map<UserId, Set<UserId>>). For getCandidates, iterate over the user's friends, then their friends, excluding the user and direct friends, using a set to deduplicate.

3. Implement recommendRandom and recommendTopK

For recommendRandom, convert the candidate set to a list and pick a random index. For recommendTopK, compute mutual friend counts for each candidate (by intersecting friend sets), then sort by count descending and user ID ascending, returning the top K.

4. Analyze complexity and optimize

Discuss time and space complexity: getCandidates O(F * avg_degree), recommendTopK O(C * avg_degree + C log C). Suggest optimizations like caching candidates or using approximate counting for large graphs.

5. Discuss production extensions

Propose extending scoring with weighted signals (e.g., interaction frequency, recency, shared groups), machine learning ranking, and scalable architectures (e.g., precomputation, sharding, streaming updates).

Key Points to Mention

  • Use of hash sets for O(1) lookups to exclude direct friends and deduplicate candidates.
  • Efficient mutual friend counting via set intersection, and sorting with a stable tie-breaker (user ID).
  • Time and space complexity analysis for each method, and potential bottlenecks.
  • Caching or precomputing friend-of-friend candidates to reduce latency in production.
  • Extending scoring with weighted features (e.g., interaction strength, profile similarity) and machine learning models.
  • Scalability considerations: sharding the graph, approximate algorithms (e.g., MinHash for similarity), and real-time vs batch processing.

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