← Uber bank

Uber · Question 1 · Arrays

Find the missing number in an array of 1 to N

ArraysPythoncode⏱ O(n)💾 O(1)
Product-based

Problem

You are given an array containing n distinct numbers taken from 0, 1, 2, ..., n. Find the one number that is missing from the array.

Approach

Use the formula: n * (n + 1) / 2 Subtract the actual sum from this expected sum.

Time O(n)Space O(1)

How it works

  1. 1The complete range 0..n sums to n*(n+1)/2.
  2. 2Subtract the actual array sum from the expected sum.
  3. 3The difference is the missing number.

Sample input / output

Input:
3
3 0 1
Output:
2

Constraints

1 <= n <= 10^4; 0 <= nums[i] <= n; all numbers distinct; exactly one missing.

Solution — read it, then walk it

Time O(n)Space O(1)Branches 1Returns 1source · Python
Scroll sideways for wide charts — the view follows the active step.
yesnoSTARTnumsn = len(nums)total_sum = n * (n + 1) //2RETURN total_sum -sum(nums)DEFINE main()import sysdata =sys.stdin.read().split()it = iter(data)n = int(next(it))nums = [int(next(it)) for _in range(n)]print(missingNumber(nums))if __name__ =="__main__"main()END
  • start / end
  • statement
  • condition
  • loop
  • returns
  • definition
1/14

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