Min Time After Which the Password Becomes Irrecoverable
Learn this problemProblem statement
Database security and authentication have become vital due to the increasing number of cyberattacks every day. Amazon has created a team for the analysis of various types of cyberattacks. In one such analysis, the team finds a virus that attacks user passwords. The virus has an attacking rule defined by attackOrder, which is a permutation of length n.
In the i-th second of the attack, the virus attacks the attackOrder[i]-th character (1-based) of the password, replacing it with the malicious character *. That is, after the i-th second, password[attackOrder[i]] = '*'.
The password is said to be irrecoverable when the number of substrings of the password containing at least one malicious character * becomes greater than or equal to m. The team wishes to find the minimum time (in seconds) after which the password becomes irrecoverable.
Note:
- If the password is irrecoverable at the start, report
1as the answer. - A substring of a string
sis a contiguous segment of that string.
Function
findMinTimeAgain(pwd: String, attackOrder: int[], m: int) → intComplete the function findMinTimeAgain in the editor.
findMinTimeAgain has the following parameters:
string password: the initial passwordint attackOrder[]: a permutation of the integers[1, 2, ..., n]giving the attack orderint m: the recoverability parameter
Returns
int: the minimum time after which the password becomes irrecoverable.
Examples
Example 1
pwd = "bcced"attackOrder = [2, 3, 1, 4, 5]m = 10return = 2There is a password of length n = 5, password = "bcced". The 1-based indices where characters will be replaced are attackOrder = [2, 3, 1, 4, 5], and the recoverability parameter is m = 10.
After the 1st second, the password becomes b*ced. The 8 substrings that contain at least one malicious character are ["b*", "b*c", "b*ce", "b*ced", "*", "*c", "*ce", "*ced"], and 8 is less than m.
After the 2nd second, the password becomes b**ed. The 11 substrings that contain at least one malicious character are ["b*", "b**", "b**e", "b**ed", "*", "**", "**e", "**ed", "*", "*e", "*ed"], and 11 is greater than or equal to m.
After the replacement at second 2, the number of such substrings is at least m. The answer is 2.
Example 2
pwd = "abcd"attackOrder = [4, 1, 3, 2]m = 10return = 4Constraints
1 <= n <= 8 * 10^5, wherenis the length ofpasswordpasswordconsists of lowercase English charactersattackOrderis a permutation of[1, 2, ..., n], so1 <= attackOrder[i] <= n0 <= m <= n * (n + 1) / 2
More Amazon problems
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026