Password Generator
Updated
Problem statement
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:
- Let
poolbe all the sets incharsetsjoined together in the given order. - If
requireEachistrue, go throughcharsetsin order and pick one character from each set (from that set only). These fill positions0tok - 1, wherekis the number of sets. - Fill every remaining position, from left to right, with a character picked from
pool. - If
requireEachistrue, shuffle the password so the required characters do not always sit at the front: forifromlength - 1down to1, take the next rollr, letj = r % (i + 1), and swap the characters at positionsiandj.
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[]) → StringExamples
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 <= 101 <= 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 ofcharsets. 1 <= length <= 1000rolls.lengthislengthwhenrequireEachisfalse, and2 * length - 1when it istrue.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