Format the Missing Numbers from 1 to 99
Updated
Problem statement
An integer array nums holds values from 1 to 99 in non-decreasing order. Return a string that lists the numbers from 1 to 99 that do not appear in nums, written in the format below.
Assumption: both 1 and 99 are part of the range, and the same number may appear in nums more than once. Build the string like this:
- Put missing numbers that follow one another, such as
4, 5, 6, into the same block. A block ends just before a number that is innumsor after99. - Write a block with one number
xasx. Write a block with two or more numbers, fromaup tob, asa-b. For example, the block4, 5is written4-5. - Write the blocks from smallest to largest, with a comma and one space between two blocks, as in
1, 4-5, 8. - When
numscontains all 99 numbers, return the empty string.
Function
missingNumbers(nums: int[]) → StringExamples
Example 1
nums = [2, 3, 6, 7, 9, 10, 99]return = "1, 4-5, 8, 11-98"The missing numbers are 1, 4, 5, 8 and every number from 11 to 98. 1 and 8 each form a block of one number. 4 and 5 follow one another, so they form the block 4-5. The last block stops at 98 because 99 is in nums.
Example 2
nums = [1, 1, 2, 50, 50, 97]return = "3-49, 51-96, 98-99"1 and 50 each appear twice, which changes nothing: a number is either in nums or missing. The missing numbers form three blocks: 3 to 49, 51 to 96, and 98 to 99 at the end of the range.
Example 3
nums = []return = "1-99"With an empty nums, all 99 numbers are missing, and they form a single block.
Constraints
0 <= nums.length <= 10001 <= nums[i] <= 99numsis sorted in non-decreasing order.
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
- JSON ParserPHONE SCREEN · Seen Jun 2021
Similar questions at other companies
- Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
- Detect a Straight FlushTemu · PHONE SCREENTemu interview questions
- Longest Common PrefixKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Shared Courses for Every Pair of StudentsCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Single-Condition Product FilterCrowdstrike · PHONE SCREENCrowdstrike interview questions
- Match a Variadic Function SignatureConfluent · PHONE SCREENConfluent interview questions
- Parse and Sort Integer DataLegora · PHONE SCREEN
- Convert an Entry List to a MapEPAM · PHONE SCREENEPAM interview questions
- Longest All-Present Attendance StreakOracle · PHONE SCREENOracle interview questions
- Most Frequent NameAmazon · PHONE SCREEN