← Google Interview Insights

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

IntermediatePrefer not to say
Jul 2026

Summary

Google SWE coding round with a classic dynamic programming problem on envelope nesting. Nothing too surprising if you've seen LIS before, but the same-width edge case is where people slip up.

Questions Asked (1)

Q1

Given n envelopes each with a width and height, find the maximum number of envelopes you can nest inside each other, where one envelope fits inside another only if both its width and height are strictly smaller.

Algorithms & Data Structures
Author's notes

The core insight is sorting by width ascending and then running longest increasing subsequence on heights.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

This is a classic dynamic programming problem that can be solved in O(n log n) by sorting envelopes by width ascending and height descending, then finding the longest strictly increasing subsequence of heights. Explain the sorting trick to avoid same-width nesting, then apply LIS using binary search for efficiency.

Pro tip: Mention the edge case of equal widths and how sorting height descending prevents invalid nesting, and discuss the trade-off between O(n^2) DP and O(n log n) LIS to show depth.

1. Understand the problem

Restate the problem: we need the maximum chain where each envelope is strictly smaller in both dimensions. Clarify that envelopes can be rotated? (No, typically not, but confirm).

2. Sort envelopes

Sort by width ascending; for equal widths, sort by height descending. This ensures that when we look for increasing heights, we don't pick two envelopes with the same width.

3. Reduce to LIS

Extract the heights array from the sorted envelopes. The problem becomes finding the longest strictly increasing subsequence (LIS) of this array.

4. Compute LIS efficiently

Use the patience sorting algorithm with binary search to compute LIS in O(n log n). Maintain a tails array where tails[i] is the smallest tail of an increasing subsequence of length i+1.

5. Return the result

The length of the tails array is the maximum number of envelopes that can be nested. Return that length.

Key Points to Mention

  • Sorting by width ascending and height descending to handle equal widths
  • Reduction to Longest Increasing Subsequence (LIS) problem
  • O(n log n) LIS using binary search (patience sorting)
  • Strictly increasing condition: both width and height must be strictly smaller
  • Edge cases: empty input, single envelope, all envelopes same size
  • Time and space complexity analysis: O(n log n) time, O(n) space

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