Password similarity under cyclic character bumps
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Amazon. It tests strings, two pointers and greedy.
A security team wants to reject a new password when it is too close to the user's previous one.
You may transform the new password like this: pick any set of its positions (possibly none) and replace each chosen character with the next letter of the alphabet, cyclically (a becomes b, m becomes n, z becomes a). Each position is bumped at most once.
The two passwords are similar if, for some choice of positions, the old password is a subsequence of the transformed new password. A subsequence keeps the original order but may skip characters.
You are given two lists of the same length, new_passwords and old_passwords. For each index i, decide whether new_passwords[i] and old_passwords[i] are similar.
Implement check_similarities(new_passwords: list[str], old_passwords: list[str]) -> list[str], returning "YES" or "NO" for each pair.
Constraints
1 <= len(new_passwords) == len(old_passwords) <= 101 <= len(new_passwords[i]), len(old_passwords[i]) <= 2 * 10^5- All passwords contain only lowercase English letters.
Example 1
new_passwords = ["abdbc"], old_passwords = ["bcd"]
Output: ["YES"]
Bump the first a to b and the following b to c, giving bcdbc. The first three characters spell bcd.
Example 2
new_passwords = ["aaa", "zb"], old_passwords = ["c", "ab"]
Output: ["NO", "YES"]
An a can only become a or b, so c never appears. In the second pair, z wraps around to a and b stays, giving ab.
Clarifying questions
- Is the old password allowed to be longer than the new one? Then the answer is
NOat once. - Is the bump only forward by one letter, so
bcan matchborcbut nevera? - Is it enough to answer yes or no, or should the chosen positions be returned?
- Is an exact match without any bumps counted as similar?
What a strong answer covers
- Sees that the bumps are independent per position, so a character
cof the new password can match an old characteroexactly wheno == coro == next(c). - Scans the new password once with a pointer into the old one, and moves the pointer whenever the current new character can match it. Taking the earliest possible match is optimal, as in a normal subsequence check.
- O(len(new) + len(old)) per pair, which is fine for
2 * 10^5characters and 10 pairs. - Gets the wraparound right:
zmatcheszora. - Tests: both examples, an old password longer than the new one, all
zs, exact equality, and a brute force over all bump choices for short strings.
Follow-ups
- Bumps can go forward or backward by one.
- A bump can move a letter forward by up to
dplaces. - Return the smallest number of bumps needed.
- Check one new password against the user's last 10 passwords quickly.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
