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
Coding & Algorithms
Software Engineer
Intel
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

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
String parsing
Range compression
Bitmasks
Sorting
Linux sysfs
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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...


Submit Your Answer
Markdown supported

Related Questions