Dsa
Hashing
Easy
Financial Ghost Transactions
An audit tool flags 'ghost transactions'. A ghost transaction is any transaction ID that appears again within K steps of its previous occurrence in the ledger. Given a sequence of transaction IDs, count how many instances are flagged as ghosts.
Input Format:
The first line contains an integer K.
The second line contains space-separated transaction ID strings.
Output Format:
An integer count of ghost transactions.
Example:
Input:
2
TX1 TX2 TX3 TX1 TX2 TX1
Output:
1
(Explanation: TX1 at index 3 is 3 steps from index 0 [3 > 2]. TX2 at index 4 is 3 steps from index 1 [3 > 2]. TX1 at index 5 is 2 steps from index 3 [2 <= 2]. So only index 5 is a ghost.)
Key concepts
hashingsliding windowdistance tracking
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now