KatInterview

Flag Account Takeovers: New Login Followed by a Large Transfer

SQL & DataAuto-graded · 15 test cases
MediumAuto-gradedSQL & Datadata-manipulationtime-windowsjoins

Last updated

Join logins, users and transfers to flag logins from a new device or IP that are followed within two hours by a large transfer to a first-time recipient.

Spot suspected account takeovers

A payments app wants to flag suspected account takeovers (ATO). You get three tables as lists of dictionaries: users, logins and transfers. Return one row for every successful login that looks like a takeover, along with the transfer that made it suspicious.

"Now" is fixed at 2025-09-01T00:00:00 (UTC). All timestamps are ISO-8601 strings without a time zone, for example "2025-08-25T10:10:00", and all are UTC.

Input

  • users: dictionaries with user_id (int) and created_at (timestamp string).
  • logins: dictionaries with user_id, ts (timestamp string), ip (string), device_id (string) and success (bool).
  • transfers: dictionaries with tx_id (int), sender_id (int), recipient_id (int), ts (timestamp string), amount_usd (decimal amount as a string such as "650.00") and status ("completed" or "reversed").

What counts as a flagged login

A login record is flagged when all of the following hold.

  1. Eligible login. It is successful, and its timestamp login_ts satisfies 2025-08-25T00:00:00 <= login_ts < 2025-09-01T00:00:00. The user must also exist in users.
  2. Account age. login_ts - created_at is at least 7 days. Logins from accounts younger than 7 days are ignored.
  3. Novelty. Look at that user's successful logins whose timestamps lie strictly between login_ts - 30 days and login_ts, with both ends excluded. Call this the lookback set.
    • The login has a new device if no login in the lookback set has the same device_id.
    • The login has a new IP if no login in the lookback set has the same ip.
    • The login must have a new device, a new IP, or both.
  4. Suspicious transfer. Only transfers with status "completed" are considered, both as candidates and as history. There must be at least one transfer that satisfies all of these:
    • sender_id equals the login's user.
    • login_ts < tx_ts <= login_ts + 2 hours (the start is excluded, the end is included).
    • amount_usd is strictly greater than 500.
    • The recipient is a first-time recipient: no completed transfer exists from the same sender to the same recipient with a timestamp strictly earlier than tx_ts.

If several transfers qualify for one login, keep only the one with the smallest (tx_ts, tx_id). Only transfers that already passed every check above compete for this.

Each qualifying login record produces exactly one output row. Two different logins may point to the same transfer, and both rows are kept.

Output

Return a list of dictionaries with exactly these keys:

  • user_id
  • login_ts: the login's original string
  • device_id
  • ip
  • reason_code: "BOTH" if the device and IP are both new, "NEW_DEVICE" if only the device is new, "NEW_IP" if only the IP is new
  • tx_id
  • tx_ts: the transfer's original string
  • amount_usd: the transfer's original value, unchanged
  • first_time_recipient_flag: always True

Sort the rows by login_ts (as a time), then user_id, then tx_ts, then tx_id, all ascending. Return an empty list if nothing qualifies.

Constraints

  • Use the fixed "now" and window above. Do not read the system clock.
  • Compare amount_usd as an exact decimal, not a float.
  • Failed logins never count as history and are never flagged.
  • Reversed transfers are ignored completely.
  • Input sizes are small enough for a straightforward solution, but grouping by user keeps it fast.

Examples

Example 1

Input: flag_account_takeover([{'user_id': 1, 'created_at': '2025-07-01T10:00:00'}, {'user_id': 2, 'created_at': '2025-08-28T09:00:00'}, {'user_id': 3, 'created_at': '2025-05-15T12:00:00'}, {'user_id': 4, 'created_at': '2025-08-01T08:30:00'}], [{'user_id': 1, 'ts': '2025-08-20T09:00:00', 'ip': '1.1.1.1', 'device_id': 'A', 'success': True}, {'user_id': 1, 'ts': '2025-08-25T10:10:00', 'ip': '2.2.2.2', 'device_id': 'B', 'success': True}, {'user_id': 1, 'ts': '2025-08-25T10:40:00', 'ip': '2.2.2.2', 'device_id': 'B', 'success': True}, {'user_id': 2, 'ts': '2025-08-29T21:00:00', 'ip': '3.3.3.3', 'device_id': 'C', 'success': True}, {'user_id': 2, 'ts': '2025-08-30T01:00:00', 'ip': '4.4.4.4', 'device_id': 'C', 'success': True}, {'user_id': 3, 'ts': '2025-08-31T15:00:00', 'ip': '5.5.5.5', 'device_id': 'D', 'success': True}, {'user_id': 3, 'ts': '2025-08-31T16:30:00', 'ip': '5.5.5.5', 'device_id': 'E', 'success': True}, {'user_id': 4, 'ts': '2025-09-01T00:10:00', 'ip': '6.6.6.6', 'device_id': 'F', 'success': False}], [{'tx_id': 101, 'sender_id': 1, 'recipient_id': 9, 'ts': '2025-08-25T11:15:00', 'amount_usd': '650.00', 'status': 'completed'}, {'tx_id': 102, 'sender_id': 1, 'recipient_id': 9, 'ts': '2025-08-26T09:00:00', 'amount_usd': '50.00', 'status': 'completed'}, {'tx_id': 103, 'sender_id': 2, 'recipient_id': 8, 'ts': '2025-08-30T01:45:00', 'amount_usd': '800.00', 'status': 'completed'}, {'tx_id': 104, 'sender_id': 2, 'recipient_id': 7, 'ts': '2025-08-27T10:00:00', 'amount_usd': '20.00', 'status': 'completed'}, {'tx_id': 105, 'sender_id': 3, 'recipient_id': 6, 'ts': '2025-08-31T16:45:00', 'amount_usd': '700.00', 'status': 'completed'}, {'tx_id': 106, 'sender_id': 3, 'recipient_id': 6, 'ts': '2025-09-01T01:00:00', 'amount_usd': '10.00', 'status': 'reversed'}, {'tx_id': 107, 'sender_id': 3, 'recipient_id': 5, 'ts': '2025-08-10T08:00:00', 'amount_usd': '5.00', 'status': 'completed'}, {'tx_id': 108, 'sender_id': 4, 'recipient_id': 5, 'ts': '2025-09-01T00:20:00', 'amount_usd': '900.00', 'status': 'completed'}])

Output: [{'user_id': 1, 'login_ts': '2025-08-25T10:10:00', 'device_id': 'B', 'ip': '2.2.2.2', 'reason_code': 'BOTH', 'tx_id': 101, 'tx_ts': '2025-08-25T11:15:00', 'amount_usd': '650.00', 'first_time_recipient_flag': True}, {'user_id': 3, 'login_ts': '2025-08-31T15:00:00', 'device_id': 'D', 'ip': '5.5.5.5', 'reason_code': 'BOTH', 'tx_id': 105, 'tx_ts': '2025-08-31T16:45:00', 'amount_usd': '700.00', 'first_time_recipient_flag': True}, {'user_id': 3, 'login_ts': '2025-08-31T16:30:00', 'device_id': 'E', 'ip': '5.5.5.5', 'reason_code': 'NEW_DEVICE', 'tx_id': 105, 'tx_ts': '2025-08-31T16:45:00', 'amount_usd': '700.00', 'first_time_recipient_flag': True}]

Hints

Hint 1

Group successful logins and completed transfers by user first, so that each login only has to scan its own user's records.

Hint 2

For the novelty test, collect the user's successful logins in the open interval (login_ts - 30 days, login_ts), then check separately whether the device and the IP appear in that set.

Hint 3

Apply the first-time-recipient filter before choosing the earliest transfer, then pick the minimum by (tx_ts, tx_id). Compare amounts with Decimal and keep the original strings for output.