Compress a Service's Port List Into Minimal Ranges
Last updated: November 29, 2026
Quick Overview
Given an unsorted list of ports (with possible duplicates) that a service definition allows, return a canonical comma-separated string where consecutive ports are collapsed into ranges, for example 80,443,8000-8002.
Illumio
November 29, 20260
0
0 solved
Given an unsorted list of ports (with possible duplicates) that a service definition allows, return a canonical comma-separated string where consecutive ports are collapsed into ranges, for example 80,443,8000-8002.
Service objects in a segmentation policy often accumulate ports from rule recommendations, and both the UI and the generated firewall rules want them in a compact canonical form. This is a warm-up in an online assessment that checks sorting, deduplication and careful handling of the final range.
What the Interviewer Expects
- Deduplicates before sorting
- Collapses only strictly consecutive ports
- Flushes the last open range after the loop
- Formats single ports without a dash
- Handles the empty input
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Extend it to also accept existing ranges like '8000-8100' as input.
- How would you compress across protocols, keeping tcp and udp separate?
- How many iptables rules does a list of k ranges need, and how does multiport change that?
- How would you diff two compressed port lists to show what a policy change opens?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of integers in 0..65535, any order, duplicates allowed. Output: a string with ascending entries; each entry is either a single port or start-end for a run of consecutive ports. Empty inp...
High-Level Design
Sort the unique ports, then walk once while tracking the start and previous value of the current run. When the next port is not prev + 1, emit the run and start a new one. Emit the final run after t...