Flag Logins From Two Countries Within One Hour

Last updated: November 29, 2026

Quick Overview

From a table of Facebook login events, find accounts that logged in from two different countries within 60 minutes of each other, returning each suspicious pair of logins once.

Meta
Data Manipulation (SQL/Python)
Data Scientist
Meta
November 29, 2026
Data Scientist
Onsite
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


From a table of Facebook login events, find accounts that logged in from two different countries within 60 minutes of each other, returning each suspicious pair of logins once.

Account security work at Meta uses impossible-travel heuristics as one signal for compromised accounts. The interviewer is testing a time-bounded self join and how you avoid returning each pair twice or matching a row to itself.

What the Interviewer Expects
  • Self joins logins on user_id with a time-window condition
  • Requires different countries
  • Uses a strict ordering (a.login_ts < b.login_ts, or id ordering) to avoid mirrored duplicates
  • Explains cost of self joins and how partitioning by user and day helps
  • Mentions VPN and travel false positives
Key Topics to Cover
self join
non-equi join
time windows
mirrored duplicates
account security
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you rewrite this with LAG to avoid the self join?
  • Would LAG miss any pairs that the self join finds?
  • How would you rank users by number of suspicious pairs?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice SQL Problems
Sample Answer
Clarify the Data

logins(login_id, user_id, login_ts, country). Login ids are unique. Two logins by the same user, in different countries, at most 60 minutes apart, form a suspicious pair. I return user_id, both lo...

Approach

Self join the table on user_id. Put the time window into the ON clause so the engine can prune. To return each unordered pair once, require the left row to come first: a.login_ts < b.login_ts, wit...


Submit Your Answer
Markdown supported