Expand a Print Dialog Page-Range String Into Page Numbers

Last updated: November 29, 2026

Quick Overview

Given a page-range string from a print dialog such as "1-3, 5, 7-9" plus the document's page count, return the sorted list of distinct pages to print, supporting open-ended ranges like "-2" and "18-" and clamping to the document.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Given a page-range string from a print dialog such as "1-3, 5, 7-9" plus the document's page count, return the sorted list of distinct pages to print, supporting open-ended ranges like "-2" and "18-" and clamping to the document.

A Google Docs or Chrome print flavored screen question about forgiving input parsing. The interviewer cares about a clear grammar, deduplication, clamping to valid pages, and how you report bad input rather than crashing.

What the Interviewer Expects
  • Defines the accepted grammar before coding
  • Splits on commas and handles single pages and ranges
  • Supports open-ended ranges and clamps to the page count
  • Deduplicates overlapping parts and returns sorted output
  • Rejects descending ranges with a clear error
Key Topics to Cover
String Parsing
Input Validation
Ranges
Sets
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
  • Go the other way: compress a sorted page list into the shortest range string.
  • The document has 10 million pages; how do you avoid materializing every page?
  • How would you report which part of the input was invalid to the user?
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: spec, comma-separated parts, each N, A-B, -B (from page 1), or A- (to the end), with optional spaces; and page_count.
  • Output: sorted, distinct page numbers within `1..page_count...
High-Level Design

Normalize by removing spaces, then split on commas. For each part, decide whether it is a range by looking for a dash, fill in missing endpoints from the document bounds, validate, clamp, and add the ...


Submit Your Answer
Markdown supported