PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Google

Generate Every Ordered Sum Using Ones and Twos

Last updated: Jul 22, 2026

Quick Overview

Generate every ordered string of ones and twos whose digit values sum to a target integer, with no duplicates and lexicographic output. The problem tests complete enumeration, deterministic ordering, small-target edge cases, and reasoning about an inherently exponential result set.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Generate Every Ordered Sum Using Ones and Twos

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

# Generate Every Ordered Sum Using Ones and Twos Given a positive integer `n`, return every string made only of the characters `"1"` and `"2"` whose digit values sum to `n`. The order of characters matters: `"12"` and `"21"` are different results. Return the strings in ascending lexicographic order, with `"1"` ordered before `"2"`. ## Function Signature ```python def ordered_sums(n: int) -> list[str]: ... ``` ## Constraints - `1 <= n <= 25` - Every returned string must have digit sum exactly `n`. - Return each valid string exactly once. ## Examples ```text Input: n = 3 Output: ["111", "12", "21"] ``` ```text Input: n = 4 Output: ["1111", "112", "121", "211", "22"] ```

Quick Answer: Generate every ordered string of ones and twos whose digit values sum to a target integer, with no duplicates and lexicographic output. The problem tests complete enumeration, deterministic ordering, small-target edge cases, and reasoning about an inherently exponential result set.

Related Interview Questions

  • Count Overlapping Rectangle Updates on a Grid - Google (hard)
  • Find A Threshold-Limited Path With Minimum Required Safety - Google (medium)
  • Filter Repeated Robot Status Messages - Google (medium)
  • Minimize Direction Violations in a Directed Road Network - Google (medium)
  • Find the Largest Monotone Increasing Number - Google (medium)
|Home/Coding & Algorithms/Google

Generate Every Ordered Sum Using Ones and Twos

Google logo
Google
Jun 17, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
4
0

Generate Every Ordered Sum Using Ones and Twos

Given a positive integer n, return every string made only of the characters "1" and "2" whose digit values sum to n.

The order of characters matters: "12" and "21" are different results. Return the strings in ascending lexicographic order, with "1" ordered before "2".

Function Signature

def ordered_sums(n: int) -> list[str]:
    ...

Constraints

  • 1 <= n <= 25
  • Every returned string must have digit sum exactly n .
  • Return each valid string exactly once.

Examples

Input: n = 3
Output: ["111", "12", "21"]
Input: n = 4
Output: ["1111", "112", "121", "211", "22"]

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google 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

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding 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.