← Questions

Longest increasing subarray and subsequence

0
MetaCodingLast asked Jul 2025

A coding interview question asked in Meta's technical phone screen, last asked Jul 2025. It tests dynamic programming, binary-search and arrays.

Two related questions on one integer array. First find the longest strictly increasing run of adjacent elements. Then find the longest strictly increasing subsequence, whose elements keep their order but need not be adjacent, and reconstruct one.

Part 1: contiguous run

Unless you agree on a different interface with the interviewer, assume:

  • longest_run(nums: list[int]) -> tuple[int, int, int] — (length, start, end) with inclusive indices of the longest strictly increasing contiguous subarray. On ties return the leftmost run. An empty array gives (0, -1, -1).
longest_run([1, 2, 1, 2, 3])  ->  (3, 2, 4)   # the run 1, 2, 3
longest_run([5, 6, 1, 2])     ->  (2, 0, 1)   # two runs of length 2; the leftmost wins
longest_run([])               ->  (0, -1, -1)

Target O(n) time and O(1) space.

Part 2: subsequence

  • lis(nums: list[int]) -> list[int] — one longest strictly increasing subsequence, as values.
lis([10, 9, 2, 5, 3, 7, 101, 18])  ->  length 4, for example [2, 3, 7, 18]
lis([0, 1, 0, 3, 2, 3])            ->  length 4, [0, 1, 2, 3]
lis([7, 7, 7])                     ->  length 1, [7]

First solve it in O(n^2), then in O(n log n).

Constraints

  • 0 <= len(nums) <= 10^5 for Part 1, <= 2,500 for the quadratic Part 2; values in [-10^9, 10^9].
  • Strictly increasing: equal values do not extend a run or a subsequence.

Clarifying questions

  • Strictly or non-strictly increasing?
  • On ties, which run or subsequence should I return?
  • Do you need the elements or only the length?

What a strong answer covers

  • Part 1: one pass tracking the start of the current run; reset when nums[i] <= nums[i-1]; update the best only when strictly longer, which keeps the leftmost on ties.
  • Part 2, quadratic: dp[i] = longest subsequence ending at i, with parent pointers for reconstruction.
  • Part 2, O(n log n): a tails array where tails[k] is the smallest tail of any increasing subsequence of length k + 1, updated with a lower-bound binary search; store each element's predecessor index to rebuild one answer.
  • Why tails itself is not a valid subsequence, and why lower bound (not upper bound) gives strictness.
  • Tests: empty, all equal, strictly decreasing, already sorted.

Follow-ups

  • Count how many longest increasing subsequences exist.
  • Longest non-decreasing subsequence: what changes in the binary search?
  • The same question on a grid: the longest strictly increasing path moving up, down, left or right.

Where it's asked

  • Meta · Technical phone screen · Last asked Jul 202545 min · Two coding problems in CoderPad, about 20 minutes each · Usually no AI; some newer screens are AI-assisted

What it tests

dynamic programming · binary-search · arrays

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding