FastPrepGenerate One-Two Strings with Sum N
Problems/Generate One-Two Strings with Sum N

Generate One-Two Strings with Sum N

Google logoGoogle● MediumNEW GRADPHONE SCREENONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source reports the one-two string enumeration and exact-complexity request but omits output order and bounds. The judged core matches the visible source at about 90%.

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

Return the strings in ascending lexicographic order, using the usual character order 1 < 2.

Function

generateOneTwoStrings(n: int) → String[]

Examples

Example 1

n = 3return = ["111","12","21"]

The valid digit sequences are 1 + 1 + 1, 1 + 2, and 2 + 1. They are already listed in lexicographic order.

Example 2

n = 4return = ["1111","112","121","211","22"]

Each returned string sums to 4. Choosing 1 before 2 during construction produces the required order.

Example 3

n = 1return = ["1"]

Only the one-character string 1 has digit sum 1.

Constraints

  • 1 <= n <= 12.
  • Every returned string contains only 1 and 2.
  • Return every valid string exactly once in ascending lexicographic order.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
public String[] generateOneTwoStrings(int n) {
    // write your code here
}
Checking account…