A security team is analyzing how quickly a virus can render a password irrecoverable.
You are given:
password of length n.attackOrder of length n, which is a permutation of the integers 1..n (1‑indexed).m.The virus attacks the password over time as follows:
attackOrder[t] by replacing it with the malicious character '*'.t:password[ attackOrder[t] ] = '*' (1‑indexed).A substring of a string is any contiguous segment of that string. The password is said to be irrecoverable once the number of substrings that contain at least one '*' is greater than or equal to m.
Your task is to compute the minimum time t (in seconds) after which the password becomes irrecoverable:
0 with the initial password), return 1.t (1 ≤ t ≤ n) such that, after applying the first t attacks, the number of substrings containing at least one '*' is ≥ m.Some online variants of this problem additionally return -1 if the password never becomes irrecoverable for any t from 1 to n, but this behavior is not specified in the core statement and is usually treated as an implementation choice.
Tags: String, Binary Search, Sliding Window
Difficulty: Medium
Source: Reconstruction from Amazon interview/online assessment variants
Assume the problem is implemented as a function:
text int findMinimumTime(string password, vector<int> attackOrder, long long m)
or equivalently in other languages.
Input
password:
n (1 ≤ n).'*' are already malicious from the start.attackOrder:
n.[1, 2, ..., n].attackOrder[t] is the position attacked at second t (1 ≤ attackOrder[t] ≤ n, all distinct).m:
long long / long depending on language).'*' for the password to be considered irrecoverable.Output
t:
1 if the password is already irrecoverable at time 0 (before any attacks).t in [1, n] such that, after applying the first t attacks, the number of substrings containing at least one '*' is at least m.t exists, -1 is returned; clarify this behavior if unspecified in a real interview.Input
password = "abcd"attackOrder = [2, 4, 1, 3] (1‑indexed)m = 5Output
1Explanation
Length n = 4.
Total number of substrings of any string of length n is:
$$
\text{totalSubstrings} = \frac{n(n+1)}{2} = \frac{4 \cdot 5}{2} = 10
$$
Initial state (time 0):
"abcd"'*' characters, so 0 substrings contain '*'.After 1 second (t = 1):
attackOrder[1] = 2."a*cd".'*': index 2.Count substrings that contain at least one '*':
'*') as:
"a" (length 1) before index 2"cd" (length 2) after index 2k, clean substrings = $$k(k+1)/2$$."a": $$1 \cdot 2 / 2 = 1$$"cd": $$2 \cdot 3 / 2 = 3$$'*':
$$
\text{maliciousSubstrings} = \text{totalSubstrings} - \text{cleanSubstrings}
= 10 - 4 = 6
$$t = 1.Since t = 1 is the smallest time at which the malicious substring count reaches at least m, return 1.
Input
password = "abcde"attackOrder = [5, 2, 4, 1, 3] (1‑indexed)m = 10Output
2Explanation
Length n = 5.
Total substrings:
$$
\text{totalSubstrings} = \frac{5 \cdot 6}{2} = 15
$$
Initial state (time 0):
"abcde", no '*'.After 1 second (t = 1):
attackOrder[1] = 5."abcd*"."abcd" (length 4)"abcd": $$4 \cdot 5 / 2 = 10$$After 2 seconds (t = 2):
5 then 2."a*cd*" (positions 2 and 5 are '*')."a" (length 1, before index 2)"cd" (length 2, between indices 2 and 5)"a": $$1 \cdot 2 / 2 = 1$$"cd": $$2 \cdot 3 / 2 = 3$$t = 2.Since it was not irrecoverable at t = 1 but is irrecoverable at t = 2, the correct answer is 2.
Input
password = "*a*"attackOrder = [2, 3, 1]m = 1Output
1Explanation
Length n = 3, total substrings:
$$
\text{totalSubstrings} = \frac{3 \cdot 4}{2} = 6
$$
Initial state (time 0):
"*a*" with malicious characters at positions 1 and 3.'*' (for example, "*" at position 1), so the number of malicious substrings is ≥ 1.By the problem requirement, if the password is irrecoverable at the start, the function should return 1.
From the problem definition and common interview variants:
n = len(password)attackOrder is a permutation of [1, 2, ..., n] (1‑indexed); all values are distinct and between 1 and n.m is an integer threshold; in interesting cases, 1 ≤ m ≤ n(n+1)/2 (the total number of substrings of a string of length n).password may already contain '*' characters; those positions are treated as malicious from time 0.Explicit numeric upper bounds on n and m are not specified in the publicly available statements of this problem. In typical coding‑interview settings, the intended solution runs in:
t (1..n), with each feasibility check in $$O(n)$$.Binary search over the time t from 1 to n, and for a fixed t simulate the first t attacks, then use a linear pass (treating maximal non‑'*' segments as sliding windows) to count clean substrings and derive the number of substrings containing at least one '*' from the total; the smallest t that meets or exceeds m is the answer.