PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/Hopper

How would you sort filenames with numeric segments?

Last updated: Mar 29, 2026

Quick Overview

This question evaluates a candidate's ability to design custom string comparators, parse maximal digit runs and compare numeric values, and handle edge cases such as leading zeros and digit-versus-non-digit ordering.

  • medium
  • Hopper
  • Coding & Algorithms
  • Software Engineer

How would you sort filenames with numeric segments?

Company: Hopper

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a list of filename strings (e.g., `"file10.txt"`, `"file2.txt"`, `"10file.txt"`). Implement a custom sort for these strings using a comparator with the following rules: 1. **Digit-first at the first differing position**: When comparing two strings from left to right, if at the first position where they differ one string has a digit (`0-9`) and the other has a non-digit, the digit one should come first. 2. **Numeric comparison for digit runs**: Treat any **maximal consecutive digit substring** as a single number and compare by **numeric value** (so `"file10.txt"` comes after `"file2.txt"`). 3. **Normal character order for non-digit runs**: Compare non-digit parts in standard character/lexicographic order. Return the filenames sorted according to these rules. Follow-ups: - **Leading zeros**: How should `"file01.txt"` and `"file1.txt"` be ordered if their numeric values are equal? Implement your chosen tie-break rule. - **Performance**: If there are very many filenames, repeatedly parsing digit runs inside the comparator can be slow. How would you optimize the sorting implementation?

Quick Answer: This question evaluates a candidate's ability to design custom string comparators, parse maximal digit runs and compare numeric values, and handle edge cases such as leading zeros and digit-versus-non-digit ordering.

Hopper logo
Hopper
Nov 6, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
4
0

You are given a list of filename strings (e.g., "file10.txt", "file2.txt", "10file.txt"). Implement a custom sort for these strings using a comparator with the following rules:

  1. Digit-first at the first differing position : When comparing two strings from left to right, if at the first position where they differ one string has a digit ( 0-9 ) and the other has a non-digit, the digit one should come first.
  2. Numeric comparison for digit runs : Treat any maximal consecutive digit substring as a single number and compare by numeric value (so "file10.txt" comes after "file2.txt" ).
  3. Normal character order for non-digit runs : Compare non-digit parts in standard character/lexicographic order.

Return the filenames sorted according to these rules.

Follow-ups:

  • Leading zeros : How should "file01.txt" and "file1.txt" be ordered if their numeric values are equal? Implement your chosen tie-break rule.
  • Performance : If there are very many filenames, repeatedly parsing digit runs inside the comparator can be slow. How would you optimize the sorting implementation?

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Hopper•More Software Engineer•Hopper Software Engineer•Hopper Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.