← Uber bank

Uber · Question 64 · Dynamic Programming

Longest Increasing Subsequence (LIS)

Dynamic ProgrammingPythoncode⏱ O(nlogn)💾 O(n)
Product-based

Problem

Given an integer array nums, return the length of the longest strictly increasing subsequence.

Approach

Greedy + Binary Search: Maintain a tails array where each element represents the minimum tail for subsequences of that length.

Time O(nlogn)Space O(n)

How it works

  1. 1Maintain tails: smallest tail per subsequence length.
  2. 2Binary-search each number's position; extend or replace.
  3. 3tails length is the LIS length.

Sample input / output

Input:
8
10 9 2 5 3 7 101 18
Output:
4

Constraints

1 <= nums.length <= 2500; -10^4 <= nums[i] <= 10^4.

Solution — read it, then walk it

Time O(nlogn)Space O(n)Loops 1Branches 3Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
yesno↻ repeatyesnoSTARTimport bisectDEFINE lengthOfLIS(nums)tails = []for x in numsi =bisect.bisect_left(tails,x)if i == len(tails)tails.append(x)ELSEtails[i] = xRETURN len(tails)DEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)n = int(next(it))nums = [int(next(it)) for _in range(n)]print(lengthOfLIS(nums))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/20

StartSTART

Interactive control-flow diagram and step-by-step code walkthrough.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits