Engineering platform~40 min4s · 256 MBtarget O((n + q) log n) time, O(n + q) spacefenwick-tree
You work on a build platform. The platform keeps a log with one failing test for each failed build, in build order. A test that fails more than once in a short stretch of builds is called flaky.
Your team lead asks: "How many tests failed at least twice between build l and build r?" All the questions are known before you start. The log can hold 100,000 builds, and there can be 100,000 questions.
Write repeat_offenders(failures, queries).
failures[i] is the id of the test that failed in build i.queries[k] = [l, r] asks about the builds l to r, both included.failures[l] to failures[r]. A test that appears three times counts once.Input: failures = [3, 1, 3, 2, 1, 3], queries = [[0, 5], [1, 4], [2, 5], [3, 3]]
Output: [2, 1, 1, 0]| Question | Failures in the stretch | Tests that appear at least twice |
|---|---|---|
[0, 5] |
3, 1, 3, 2, 1, 3 | 3 and 1, so 2 |
[1, 4] |
1, 3, 2, 1 | 1, so 1 |
[2, 5] |
3, 2, 1, 3 | 3, so 1 |
[3, 3] |
2 | none, so 0 |
Input: failures = [7], queries = [[0, 0]]
Output: [0]One failure cannot repeat.
Input: failures = [4, 4, 4, 4], queries = [[0, 3], [1, 1], [1, 2]]
Output: [1, 0, 1]One test fails in every build. It counts once when the stretch has two or more builds.
1 ≤ len(failures) ≤ 100,0001 ≤ failures[i] ≤ 1,000,000,0001 ≤ len(queries) ≤ 100,0000 ≤ l ≤ r < len(failures).Counting the failures of a stretch costs up to 100,000 steps, and 100,000 questions cost 10 billion steps. That is too slow. The questions are known in advance, so you can answer them in a smart order. About log n steps for each build and each question is enough.