Consecutive Smaller Run Length
Updated
Problem statement
You are given an integer array nums. For every index i, find how long the run of smaller values that ends at i is.
Start at index i and step left one index at a time. Keep stepping while the value you reach is strictly smaller than nums[i]. Stop when you reach a value that is equal to or larger than nums[i], or when there is nothing more to the left.
answer[i] is the number of values you stepped over plus 1 for nums[i] itself. So answer[0] is always 1. Return the array answer.
Function
smallerRunLengths(nums: int[]) → int[]Examples
Example 1
nums = [3,1,2,5,4,4,6]return = [1,1,2,4,1,1,7]Look left from each number and count the numbers right before it that are smaller, then add 1 for the number itself.
- Index 0 (3): nothing is to its left, so the answer is 1.
- Index 1 (1): 3 is not smaller than 1, so the answer is 1.
- Index 2 (2): 1 is smaller, then 3 is not. The answer is 1 + 1 = 2.
- Index 3 (5): 2, 1 and 3 are all smaller. The answer is 3 + 1 = 4.
- Index 4 (4): 5 is not smaller, so the answer is 1.
- Index 5 (4): the 4 right before it is equal, not smaller, so the answer is 1.
- Index 6 (6): 4, 4, 5, 2, 1 and 3 are all smaller. The answer is 6 + 1 = 7.
Example 2
nums = [-2,-5,0,0,3]return = [1,1,3,1,5]Index 2 (0) counts -5 and -2, so its answer is 3. Index 3 (0) stops at once, because the 0 before it is equal, so its answer is 1. Index 4 (3) counts 0, 0, -5 and -2, so its answer is 5.
Example 3
nums = [10,20,30]return = [1,2,3]Each number is larger than every number before it. So the answers are 1, 2 and 3.
Constraints
1 <= nums.length <= 5000-1000000000 <= nums[i] <= 1000000000
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Asteroid CollisionSiemens · PHONE SCREEN
- Billing Log with Undo and RedoReddit · PHONE SCREENReddit interview questions
- Compress Editor Actions into Operational Transform OperationsReplit · OA · PHONE SCREEN · ONSITE INTERVIEW
- Daily TemperaturesCARS24 · PHONE SCREEN
- Next Greater Indices and Maximum GapTikTok · PHONE SCREENTikTok interview questions
- Next Greater Value in a Circular ArrayPayPay · PHONE SCREENPayPay interview questions
- Stack with Constant-Time Middle QueriesAmazon · PHONE SCREEN
- Generic Stack Operation SequenceArista Networks · PHONE SCREENArista Networks interview questions
- Stock Span IFreshworks · PHONE SCREENFreshworks interview questions
- Exclusive Function Execution TimeDecagon · PHONE SCREENDecagon interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)