Expand and Compress Linux CPU Affinity List Strings
Last updated: November 29, 2026
Quick Overview
Parse a Linux-style CPU list such as "0-3,8,10-11" into a sorted list of logical CPU ids, and do the reverse: compress an unsorted list of CPU ids into the shortest range string, plus convert it to a bitmask.
Intel
November 29, 20260
0
0 solved
Parse a Linux-style CPU list such as "0-3,8,10-11" into a sorted list of logical CPU ids, and do the reverse: compress an unsorted list of CPU ids into the shortest range string, plus convert it to a bitmask.
Intel engineers working on Linux enablement, power management and hybrid P-core/E-core scheduling deal with cpulist strings in sysfs and taskset all day. This screen checks careful string parsing, range compression and comfort with bitmasks.
What the Interviewer Expects
- Splits on commas and handles both single ids and lo-hi ranges
- Deduplicates and sorts the result
- Compresses consecutive runs into ranges in one linear pass
- Validates malformed input such as reversed ranges
- Converts a set of CPUs to a bitmask with shifts
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
- How would you pin a process to only the P-cores given a separate map of core types?
- The machine has 512 logical CPUs. How does that affect the bitmask representation?
- How would you support the stride syntax 0-15:2 that the kernel also accepts?
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 pieces are separated by commas; each is either n or lo-hi inclusive. Output for parsing is a sorted list with no duplicates. Formatting must produce minimal ranges, for example `[5,1,2,3,7,8...
High-Level Design
Parsing: split, strip, branch on -, add to a set. Formatting: sort unique ids, then walk with two pointers, extending j while the next id is consecutive, and emit i alone or i-j. The mask is t...