Generate One-Two Strings with Sum N
Updated
Problem statement
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
1and2. - Return every valid string exactly once in ascending lexicographic order.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Permutations IILinkedIn · PHONE SCREEN
- Unique String PermutationsAmazon · PHONE SCREEN
- Unique Digit Permutations Without Leading ZeroMicrosoft · ONSITE INTERVIEW
- Winning Draws for a Mahjong HandZip · PHONE SCREEN
- Count Vowel PermutationsZscaler · ONSITE INTERVIEW
- Palindrome PartitioningLondon Stock Exchange · PHONE SCREEN
- Word Break IIWex · PHONE SCREEN
- Word Break IIInMobi · PHONE SCREEN
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEW
- Replace '?' to Avoid Adjacent DuplicatesSalesforce · OA