FastPrepPassword Generator
Problems/Password Generator

Password Generator

Patreon logoPatreon● EasyFULLTIMEINTERNPHONE SCREENONSITE INTERVIEW

Updated Dec 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Four reports from 2018 to 2022 give a password generator as a Patreon coding question. A summer 2021 intern candidate had it in the coding round of a virtual onsite on 2020-10-09: random passwords from small letters, capital letters and digits, then follow-ups that leave out capitals or small letters, demand special characters, demand one capital, one small letter and one digit at minimum, fix the length, and ask how to test a password against the rules. A March 2022 Site Reliability Engineer report adds a text menu that turns symbols, capitals, small letters and numbers on or off, making sure each kind appears, and the security risks; it does not tie the task to a round, so we take it to be the technical round that came before the four-hour final round. A November 2018 Senior Site Reliability Engineer phone screen had this as its single question, done in fifteen minutes: random strings from a character set, with sets that can be switched on or off. A November 2018 Senior Software Engineer report, whose process was first a phone screen, then one more technical round, and which does not name the round for the task, lists making passwords with a random library and keeping them complex enough. Stage: onsite (stated) for 2020, phone screen (stated) for the 2018 reliability role, phone screen (inferred) for 2022, phone screen as the neutral default for the 2018 software role. Our choices: the function gets only the sets that are on, as a list of strings; the text menu stays out of the code; the random numbers come in as an array used in order, r % size picking a character, so answers can be checked; required characters come first, the rest from all sets joined, then a backward swap shuffle; the empty string means no set is on, or the length is below the number of required sets; characters and limits are ours, as are the examples.

You are writing a password generator. A user switches character sets on or off, and charsets holds the sets that are on, in a fixed order; a set that is off is simply not in the list. The password must have exactly length characters. When requireEach is true, every set in charsets must also show up in the password at least once.

A real generator draws random numbers. So that your answer can be checked, the random numbers are given to you in rolls. Use them one at a time, in order, starting with rolls[0]. To pick a character from a string s with the next roll r, take s[r % s.length].

Build the password in these steps:

  1. Let pool be all the sets in charsets joined together in the given order.
  2. If requireEach is true, go through charsets in order and pick one character from each set (from that set only). These fill positions 0 to k - 1, where k is the number of sets.
  3. Fill every remaining position, from left to right, with a character picked from pool.
  4. If requireEach is true, shuffle the password so the required characters do not always sit at the front: for i from length - 1 down to 1, take the next roll r, let j = r % (i + 1), and swap the characters at positions i and j.

Return the password. If charsets is empty, or requireEach is true and length is smaller than the number of sets, no valid password exists: return the empty string "" instead.

Function

generatePassword(charsets: String[], length: int, requireEach: boolean, rolls: int[]) → String

Examples

Example 1

charsets = ["abcdefghijklmnopqrstuvwxyz", "0123456789"]length = 5requireEach = falserolls = [7, 30, 2, 35, 61]return = "h4c9z"

Small letters and digits are on, so pool holds 26 letters followed by 10 digits, 36 characters in all. requireEach is false, so each roll picks straight from pool: 7 gives 'h', 30 gives the digit at index 4 of the digits, '4', 2 gives 'c', 35 gives '9', and 61 % 36 = 25 gives 'z'. No shuffle is done.

Example 2

charsets = ["abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789", "!@#$%"]length = 5requireEach = truerolls = [4, 27, 3, 6, 0, 0, 1, 2, 0]return = "@a3Be"

All four sets are required. The first four rolls pick one character from each set: 4 gives 'e', 27 % 26 = 1 gives 'B', 3 gives '3', and 6 % 5 = 1 gives '@'. The fifth roll, 0, fills the last position from pool with 'a', so before the shuffle the password is "eB3@a". The last four rolls shuffle it. For i = 4, j = 0 % 5 = 0, giving "aB3@e". For i = 3, j = 1 % 4 = 1, giving "a@3Be". For i = 2, j = 2 % 3 = 2, so nothing moves. For i = 1, j = 0 % 2 = 0, giving "@a3Be".

Example 3

charsets = ["abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "0123456789"]length = 2requireEach = truerolls = [5, 8, 13]return = ""

Three sets are required, but the password has only 2 positions, so no valid password exists and the answer is the empty string. The rolls are not used.

Constraints

  • 0 <= charsets.length <= 10
  • 1 <= charsets[i].length <= 26
  • Each character of charsets[i] is an English letter, a digit, or one of !@#$%^&*-_+=?, and no character appears more than once across all of charsets.
  • 1 <= length <= 1000
  • rolls.length is length when requireEach is false, and 2 * length - 1 when it is true.
  • 0 <= rolls[i] <= 109

More Patreon Phone Screen questions

  • Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
  • Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
  • Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
  • Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
  • Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
  • Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
  • Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
  • LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
  • JSON ParserPHONE SCREEN · Seen Jun 2021

Similar questions at other companies

  • Container Ledger with TradesFlexport · ONSITE INTERVIEWFlexport interview questions
  • Christmas Tree with N TiersVerily · ONSITE INTERVIEW
  • Search and Format ContactsAdyen · PHONE SCREENAdyen interview questions
  • Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
  • Portfolio Valuation, Part 1Stripe · PHONE SCREENStripe interview questions
  • Format a Signed Integer with CommasHudson River Trading · PHONE SCREENHudson River Trading interview questions
  • Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
  • Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
  • Run-Length DecodingApple · PHONE SCREENApple interview questions
  • Trace Pointer Rebinding and Pointee UpdatesArista Networks · PHONE SCREENArista Networks interview questions
All Patreon interview questionsSee Patreon hiring insights
Saved
class Solution {
    public String generatePassword(String[] charsets, int length, boolean requireEach, int[] rolls) {
        // Write your code here.
    }
}
Checking account…