File Download Coverage Percentage
Reported by candidates from Persona's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Persona reportedly asked this one in September 2023, and the detail that trips people is the flag: requireTwice flips the whole answer from a percentage to an all-or-nothing 100 or 0. You've got a file with up to 100000 chunks and up to 100000 inclusive intervals, so a naive loop over every chunk per interval dies. It's a difference array problem in disguise. If you blank during the live OA, StealthCoder sits invisible on your screen as a safety net, but this one is short enough to own before you sit down.
The problem
A file has chunks numbered 1..totalChunks. Each inclusive interval records one completed download of every chunk in that range. If requireTwice is false, return the integer percentage of chunks downloaded at least once, rounded down. If it is true, return 100 only when every chunk was downloaded at least twice; otherwise return 0. Function downloadCoveragePercent(totalChunks: int, intervals: int[][], requireTwice: boolean) → int Examples Example 1 totalChunks = 5 intervals = [[3,4]] requireTwice = false return = 40 Two of five chunks were downloaded. Example 2 totalChunks = 5 intervals = [[1,5],[1,5]] requireTwice = true return = 100 Every chunk appears in both intervals. Example 3 totalChunks = 5 intervals = [[1,5],[2,5]] requireTwice = true return = 0 Chunk 1 was downloaded only once. Constraints 1 <= totalChunks <= 100000. 0 <= intervals.length <= 100000. 1 <= start <= end <= totalChunks.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a difference array. For each interval [s,e], add 1 at index s and subtract 1 at index e+1. Then one prefix-sum pass gives the download count for every chunk in O(totalChunks + intervals). Size the array totalChunks+2 so e+1 never goes out of bounds. Then branch on the flag. If requireTwice is false, count chunks with coverage >= 1 and return count*100/totalChunks using integer division so it rounds down. If true, return 100 only when the minimum coverage across all chunks is >= 2, else 0. Common pitfalls: looping each interval chunk by chunk and timing out, forgetting the empty intervals case (answer 0 for the false branch), and using float division that rounds wrong. Multiply before dividing. StealthCoder is your hedge if the prefix-sum setup slips your mind mid-assessment.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill File Download Coverage Percentage cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Persona's OA.
Persona reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
File Download Coverage Percentage FAQ
What's the trick in the Persona file download coverage problem?+
Use a difference array. Add 1 at each interval start and subtract 1 at end+1, then take a running prefix sum. That gives how many times each chunk was downloaded in linear time, no matter how many intervals overlap.
How hard is this really?+
Easy to medium. The logic is simple once you know difference arrays. The constraints of 100000 chunks and 100000 intervals just rule out brute force. If you've seen range-update problems, it's a ten minute solve.
How do I handle the rounding down?+
Use integer math: covered * 100 / totalChunks with integer division. Multiply first, then divide. Avoid floating point, since it can introduce errors and you'd then have to floor the result anyway.
What happens when requireTwice is true?+
The answer is binary. Return 100 if every chunk from 1 to totalChunks has a count of at least 2 after the prefix sum, otherwise 0. Partial coverage doesn't count, as Example 3 shows with chunk 1 downloaded only once.
What edge cases should I test before submitting?+
Test an empty intervals array, which should return 0 in both modes. Test an interval ending at totalChunks to confirm your array has room for end+1. Test totalChunks of 1. Also check that the percentage truncates, such as 1 of 3 chunks giving 33.