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

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report asks to list the strings that sum to one-two, and asks for the exact complexity. It does not give the output order or the input limits. Our version is about 90% the same as the reported question.

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 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

Interview reports that mention this question

  • Google L4 PhD New Grad Europe Onsite: 1 Experience

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
See Google hiring insights
Saved
class Solution {
    public String[] generateOneTwoStrings(int n) {
        // write your code here
    }
}
Checking account…