The Password Builder
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L3
- Asked in
- Rally Health
Problem
Given a positive integer length and a non-empty string pool, return a string of length characters where position i is pool[i % len(pool)].
Random characters, fixed rules.
Example
Input:
length = 8, pool = "abcXYZ"
Output:
"abcXYZab"
Worked solution and explanation
Why this problem exists in real interviews
This tests modular arithmetic for cyclic indexing, a basic but essential skill. It checks whether candidates can cycle through a character pool using the modulo operator without falling back to complex conditional logic.
Break down the requirements
Step 1: Iterate for the requested length
Loop from 0 to length-1, building one character per iteration.
Step 2: Index the pool with modulo
Use pool[i % len(pool)] to wrap around when i exceeds the pool length.
Step 3: Join and return the result
Collect characters in a list and join them into a string.
The solution
def generate_password(length, pool):
chars = []
for i in range(length):
chars.append(pool[i % len(pool)])
result = ''.join(chars)
return resultCommon follow-up questions
- What if the pool were empty? (Tests guarding against division by zero in the modulo.)
- How would you shuffle the characters to make the password less predictable? (Tests using random.shuffle or random.choice.)
- What if certain characters must appear at least once? (Tests placing required characters first, then filling the rest.)