← Questions

Password similarity under cyclic character bumps

0
AmazonCoding

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) <= 10
  • 1 <= 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 NO at once.
  • Is the bump only forward by one letter, so b can match b or c but never a?
  • 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 c of the new password can match an old character o exactly when o == c or o == 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^5 characters and 10 pairs.
  • Gets the wraparound right: z matches z or a.
  • 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 d places.
  • 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

strings · two pointers · greedy · subsequence

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding