How Many Numbers Are Smaller Than the Current Number

EasyArrays & Hashingprefix sum

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array nums, build an array result of the same length where result[i] is the number of indices j with nums[j] < nums[i]. Equal values do not count as smaller. Because the values are bounded by 100, you can do this in O(n + 100) time with a counting array and prefix sums.

Examples

Input: nums = [8,1,2,2,3]
Output: [4,0,1,1,3]
Four values are below 8; none are below 1; only 1 is below each 2; 1, 2, 2 are below 3.
Input: nums = [7,7,7,7]
Output: [0,0,0,0]
Equal values are not smaller.
Input: nums = [5,0,10,3]
Output: [2,0,3,1]

Constraints

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More prefix sum problems