← Snapchat Interview Insights

Snapchat·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Snapchat software engineering interview that went deep on a grid-based optimization problem, then kept pushing into dynamic data structure design and complexity analysis. More theoretical than I expected, felt like a systems/algorithms hybrid the whole way through.

Questions Asked (3)

Q1

Given an m×n grid where cells are marked 1 for homes and 0 otherwise, find a single meeting cell that minimizes the total Manhattan distance from all homes to that cell. Return the minimum total distance and one valid optimal cell.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I knew Manhattan distance problems lean on medians pretty quickly, so I jumped to projecting rows and columns separately.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Recognize that Manhattan distance separates into independent x and y coordinates, so the optimal meeting cell's row and column can be chosen independently as the medians of the homes' row and column coordinates. Then compute the total distance by summing distances from all homes to that cell, and return the minimum total distance along with one optimal cell.

Pro tip: Mention that if there are multiple optimal cells (e.g., when the number of homes is even), any cell within the median interval works, and you can pick the smallest row and column medians for determinism. Also, note that the solution generalizes to weighted homes by using weighted medians.

1. Clarify the problem and constraints

Confirm that the meeting cell can be any cell in the grid (including a home) and that distance is Manhattan. Ask about grid size, number of homes, and whether multiple optimal cells are acceptable.

2. Separate dimensions

Explain that Manhattan distance decomposes into independent row and column components, so minimizing the sum is equivalent to minimizing the sum of row distances plus the sum of column distances separately.

3. Find optimal row and column

Collect all home row indices and column indices. The optimal row is any median of the row indices, and the optimal column is any median of the column indices. For even counts, any value between the two middle values works.

4. Compute total distance

Choose one optimal cell (e.g., the smallest median row and column). Iterate through all homes and sum the Manhattan distances to that cell. Return the sum and the cell coordinates.

5. Analyze complexity and edge cases

State that the algorithm runs in O(mn) time to collect homes and O(k) to compute distance, where k is the number of homes. Discuss edge cases: no homes, one home, all homes in a line, and multiple optimal cells.

Key Points to Mention

  • Manhattan distance decomposes into independent x and y coordinates.
  • The optimal meeting point is the median of the homes' coordinates in each dimension.
  • For an even number of homes, any point between the two middle coordinates is optimal.
  • Time complexity: O(mn) to scan the grid, O(k) to compute distance, where k is the number of homes.
  • Space complexity: O(k) to store home coordinates, or O(1) extra if computing medians on the fly with two passes.
  • The solution can be extended to weighted homes by using weighted medians.

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

Q2

Extend the static grid solution to a dynamic system supporting addHome, removeHome, and query operations that return the optimal cell and minimum distance. What data structures would you use and what are the time and space complexities for each operation?

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

This is where I got a little lost.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: what operations are needed, expected frequency, and constraints. Then propose a data structure that balances update and query costs, such as a balanced BST or a heap with lazy deletion, and analyze the time and space complexities for each operation. Finally, discuss trade-offs and potential optimizations like caching or using a segment tree for range queries.

Pro tip: Mention that in a real system like Snapchat, you'd likely need to handle concurrent updates and queries, so consider thread-safe data structures or locking strategies. Also, discuss how the solution scales with a large number of homes and queries.

1. Clarify Requirements

Ask about the expected number of homes, frequency of add/remove/query operations, and whether the grid is fixed-size or dynamic. Confirm if the query returns the optimal cell (e.g., minimizing distance to all homes) and what distance metric is used.

2. Choose Data Structures

Propose a data structure that supports efficient updates and queries. For example, a balanced binary search tree (BST) keyed by coordinates, or a heap for each dimension, or a segment tree if the grid is small. Consider using a hash map for quick lookups and a priority queue for nearest neighbor queries.

3. Analyze Complexities

For each operation (addHome, removeHome, query), state the time complexity (e.g., O(log n) for BST operations, O(1) for hash map) and space complexity (O(n) for storing homes). If using a heap with lazy deletion, note that removeHome is O(1) but query may be O(n) in worst case.

4. Discuss Trade-offs

Compare different approaches: e.g., a simple list gives O(1) add but O(n) query; a BST gives O(log n) for all but may have overhead. Mention that the optimal choice depends on the read/write ratio and whether the grid is dense or sparse.

5. Optimize and Extend

Suggest optimizations like caching frequent queries, using a k-d tree for multidimensional nearest neighbor, or employing a segment tree for range minimum queries. Also, consider concurrency and scalability if this is a distributed system.

Key Points to Mention

  • Distance metric (e.g., Manhattan, Euclidean) and how it affects the data structure choice.
  • Use of balanced BST (e.g., AVL, Red-Black) for O(log n) add/remove/query.
  • Heap with lazy deletion: O(1) add, O(1) remove (mark as deleted), O(n) query worst-case.
  • Segment tree or Fenwick tree for range queries if the grid is 1D or can be flattened.
  • k-d tree for multidimensional nearest neighbor queries with average O(log n) but worst-case O(n).
  • Concurrency considerations: locking, read-write locks, or lock-free data structures for high throughput.

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

Q3

Why does this approach use the median rather than the average to minimize total Manhattan distance, and how would you handle large sparse grids where most cells are empty?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The median vs average explanation felt like a relief after the dynamic design struggle.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Explain that the median minimizes the sum of absolute deviations because the objective function is piecewise linear and convex, with the derivative changing sign at the median. Then discuss how to handle large sparse grids by storing only non-empty cells in a hash map or sorted lists, and computing the median via selection algorithms or order statistics without materializing the full grid.

Pro tip: Mention that for even counts, any value between the two middle elements minimizes the sum, and that in practice you can pick either median. Also note that sparse grids often require coordinate compression or using a balanced BST to maintain order statistics efficiently.

1. Explain why median minimizes Manhattan distance

State that the total Manhattan distance is the sum of absolute differences in each dimension, and for a set of points, the median minimizes the sum of absolute deviations. Provide a brief mathematical justification: the derivative of the sum of absolute values is the count of points less than x minus the count greater than x, which is zero at the median.

2. Address the 1D case and extension to 2D

Clarify that in 2D Manhattan distance, the x and y coordinates are independent, so the optimal point has x-coordinate equal to the median of all x-coordinates and y-coordinate equal to the median of all y-coordinates. This separability is key.

3. Discuss handling large sparse grids

Explain that instead of iterating over all cells, store only the non-empty cells (e.g., in a hash map or list). To find the median efficiently, use a selection algorithm like Quickselect or maintain a balanced binary search tree for dynamic updates.

4. Consider trade-offs and optimizations

Mention that if the grid is static, sorting the coordinates and picking the middle element is O(n log n). If dynamic, a Fenwick tree with coordinate compression can support updates and median queries in O(log n). Also note that for very large grids, memory can be saved by not storing empty cells.

Key Points to Mention

  • Median minimizes sum of absolute deviations; average minimizes sum of squared deviations.
  • Manhattan distance separates into independent x and y components.
  • For even number of points, any value between the two middle values is optimal.
  • Sparse grids: store only non-empty cells using hash maps or lists.
  • Use selection algorithms (Quickselect) or order-statistic trees for efficient median finding.
  • Coordinate compression and Fenwick trees for dynamic updates and median queries.

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