FastPrepCompact the List
Problems/Compact the List

Compact the List

Cisco logoCisco● EasyINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

You are given a list of integers inputList of size inputListSize. The list is sorted in ascending order and has no duplicates.

Compact the list: replace every run of two or more consecutive integers (each one exactly 1 more than the one before) with the string "a to b", where a is the first number of the run and b the last. A number that is not part of such a run stays as its own string, such as "10".

Return the resulting strings in order. If there are no runs, every number becomes its own string.

Function

compactList(inputListSize: int, inputList: int[]) → String[]

Examples

Example 1

inputListSize = 8inputList = [1, 2, 3, 6, 7, 8, 10, 15]return = ["1 to 3", "6 to 8", "10", "15"]

In the given list 1,2,3 form a continuous range and hence are compacted to "1 to 3" and the same for 6,7,8 which are compacted to "6 to 8". But 10 and 15 cannot be compacted so are printed as they are.

Constraints

  • 1 ≤ inputListSize ≤ 10^5
  • inputList.length == inputListSize
  • -10^9 ≤ inputList[i] ≤ 10^9
  • inputList is strictly increasing.

More Cisco interview questions

  • Check if a String Is a PalindromePHONE SCREEN · Seen Nov 2026
  • Find Unique ElementsPHONE SCREEN · Seen Nov 2026
  • First Letter to Appear TwicePHONE SCREEN · Seen Nov 2026
  • Middle of a Linked ListPHONE SCREEN · Seen Nov 2026
  • Reverse a Linked ListPHONE SCREEN · Seen Nov 2026
  • Reverse a StringPHONE SCREEN · Seen Nov 2026
  • Valid ParenthesesPHONE SCREEN · Seen Nov 2026
  • K-Step Bit Flip with RetentionPHONE SCREEN · Seen Nov 2026
  • Todo Task Manager OperationsONSITE INTERVIEW · Seen Oct 2026
  • Find the Duplicate Number Without Modifying the ArrayONSITE INTERVIEW · Seen Sep 2026

Similar questions at other companies

  • Total Server DowntimeVisa · OAVisa interview questions
  • Signal FilterJP Morgan Chase · OAJP Morgan Chase interview questions
  • Merge IntervalsApple · OA · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
  • Earliest Meeting Time for All EmployeesCapital One · OACapital One interview questions
  • Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
  • Maximum Interactive Team SizeMicrosoft · OAMicrosoft interview questions
  • Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
  • Largest Team Where Every Pair OverlapsWalmart · OAWalmart interview questions
  • Maximum Compatible Time WindowsIBM · OAIBM interview questions
  • Smallest Most-Lit Street PositionHudson River Trading · OAHudson River Trading interview questions
All Cisco interview questionsSee Cisco hiring insights
Saved
class Solution {
    public String[] compactList(int size, int[] inputList) {
      // write your code here
    }

}
Checking account…