Compress Consecutive Integer Ranges
Updated
Problem statement
Given an unsorted integer array nums, return a compact string describing its distinct values in ascending order.
Sort the values and split them into maximal runs of consecutive integers. Format a run containing at least two distinct values as start-end, format a singleton as its decimal value, and join the pieces with commas and no spaces. Repeated occurrences of a value belong to the same run and appear only once in the result. Return the empty string for an empty array.
Function
compressRanges(nums: int[]) → StringExamples
Example 1
nums = [1,2,5,3,6,9]return = "1-3,5-6,9"Sorting gives [1,2,3,5,6,9]. The maximal runs are 1 through 3, 5 through 6, and the singleton 9.
Example 2
nums = [1,3,5,7,9]return = "1,3,5,7,9"No two distinct values are consecutive, so every value is a singleton.
Example 3
nums = [-3,-2,-2,-1,2,3]return = "-3--1,2-3"The duplicate -2 is ignored. The two maximal runs are -3 through -1 and 2 through 3.
Constraints
0 <= nums.length <= 200000.- Every element fits in a signed 32-bit integer.
- The method may reorder
nums. - Repeated values are emitted once.
More DocuSign Onsite Interview questions
- Count Binary SubstringsONSITE INTERVIEW · Seen Sep 2026
- Two Positions That Add Up to a TargetPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- JSON Schema Object ValidatorONSITE INTERVIEW · Seen Aug 2026
- Most Transactions Kept Without Going NegativeONSITE INTERVIEW · Seen May 2025
- Three-Part Splits With a Light MiddleONSITE INTERVIEW · Seen May 2025
- Combine Two Sorted Lists Into OneONSITE INTERVIEW · Seen Apr 2022
- Exact Palindrome CheckPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Paired Words That Share a SubstringONSITE INTERVIEW · Seen Apr 2022
- Reverse the Characters of a TextPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Reverse the Order of a Linked ListONSITE INTERVIEW · Seen Feb 2020
Similar questions at other companies
- Map and Accumulate Learning QueriesPalantir · ONSITE INTERVIEWPalantir interview questions
- Meeting RoomsGoldman Sachs · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Filter Anagrams of a TargetMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Deduplicate Logs: Keep FirstGoogle · ONSITE INTERVIEWGoogle interview questions
- Loyal Customers by Two-Day Product VarietyAmazon · ONSITE INTERVIEW
- Type-Ahead Prefix MatchesNavan · ONSITE INTERVIEWNavan interview questions
- Count Word FrequenciesBloomberg LP · ONSITE INTERVIEWBloomberg LP interview questions
- Most Voted CandidateExpedia · ONSITE INTERVIEWExpedia interview questions
- Anagrams of a Word in a Word ListZillow · PHONE SCREEN · ONSITE INTERVIEWZillow interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions