Location of Data After Transfers
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
You're tracking data movements across Amazon servers through a year of transfers. X starts with data at n locations, then moves it m times. Each move is from one location to another. Your job: report which locations have data after all moves complete, in ascending order. This is a simulation problem wrapped in a tracking problem. You need to follow the chain of movements and figure out where data actually ends up. StealthCoder can catch your logic if you blank on the traversal.
The problem
X stores its data on different servers at different locations. From time to time, due to several factors, X needs to move its data from one location to another. This challenge involved keeping track of the locations of X's data and report them at the end of the year. At the start of the year, X's data was located at n different locations. Over the course of the year, X's data was moved from one server to another m times. Precisely, in the ith operation, the data was moved from movedFrom[i] to movedTo[i]. Find the locations of the data after all m moving operations. Return the locations in ascending order. Note: It is guaranteed that for any movement of data : Returns int[]: the locations storing data after all moves are made, in ascending order.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick here is that data doesn't disappear when it moves. If data starts at location A, moves to B, then to C, it ends at C but it was never at A or B at the end. You're simulating a sequence of transfers. Build a set of active locations. Start with all n original locations. For each transfer, remove the source and add the destination. Watch out: if the same location receives data twice, it should only appear once in the result. The pattern is simulation with a set or hash table to track state. Edge case: a location could be a source multiple times. Sort the final set and return. StealthCoder's real value here is catching off-by-one errors or forgetting to handle duplicate destinations when you're under pressure during the live OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Location of Data After Transfers 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Location of Data After Transfers FAQ
Do locations ever get data back after they've been emptied?+
Yes. If location A moves to B, then B moves back to A, location A is active again. Track the current state after each move, not the history. Use a set and update it for every operation.
What if data moves from location X to location X?+
The problem doesn't explicitly forbid it. Handle it gracefully: remove X, add X back. Net result: X stays active. It's a no-op, but don't crash on it.
Can multiple locations hold data at the end?+
Yes. Data doesn't consolidate unless explicitly moved. If you start with locations 1, 2, 3 and only move from 1 to 4, you end with 2, 3, 4. Track all active locations.
Is there a trick with the sorting requirement?+
No trick. The sorting is just output format. Build your set, convert to a sorted list. Python's sorted() or a TreeSet works. Don't overthink it.
How fast does this need to run for the Amazon OA?+
O(m log m) for the sort is fine. O(m) to process moves is required. If n and m are both under 10k, a hash set solution will never timeout. Focus on correctness.