KatInterview

Users Logging In Successfully From Several Countries

SQL & DataAuto-graded · 18 test cases
EasyAuto-gradedSQL & Datahash-mapsetsfiltering

Last updated

Group login records by user, keep only exact-match successful ones, and report users whose successful logins span at least two distinct countries, sorted.

Multi-Country Successful Logins

You are given a log of authentication events. Each event is a dictionary with these string fields:

  • user_id: the account that attempted to log in
  • timestamp: when the attempt happened (informational only, not needed for the answer)
  • status: the outcome of the attempt
  • country: the country the attempt came from

Write a function that returns the IDs of all users who have successfully logged in from two or more different countries.

Rules

  • An event counts as a success only if its status is exactly the string "SUCcEss", with this precise mix of upper and lower case. Any other value (for example "success", "SUCCESS", "FAILED") is not a success and must be ignored completely.
  • Countries are compared as exact strings, with no trimming or case folding. "US" and "us" would be different countries.
  • Only successful events contribute countries. A country seen only in non-successful events does not count toward the user.
  • Many successes from the same country count as just one country.
  • Return each qualifying user_id exactly once, sorted in ascending order using standard Python string comparison.
  • If no user qualifies, or the input is empty, return an empty list.

Constraints

  • The input is a list of dictionaries, each containing the keys user_id, timestamp, status, and country, all with string values.
  • The list may be empty.
  • A single user may appear in many events, with any mix of statuses and countries.
  • The result is a list of strings.

Examples

Example 1

Input: users_with_multi_country_successes([{'user_id': 'alice', 'timestamp': '2024-01-01 09:00:00', 'status': 'SUCcEss', 'country': 'US'}, {'user_id': 'alice', 'timestamp': '2024-01-02 09:00:00', 'status': 'SUCcEss', 'country': 'GB'}, {'user_id': 'bob', 'timestamp': '2024-01-01 10:00:00', 'status': 'SUCcEss', 'country': 'US'}])

Output: ['alice']

Example 2

Input: users_with_multi_country_successes([{'user_id': 'zoe', 'timestamp': '2024-02-01 12:00:00', 'status': 'SUCcEss', 'country': 'CA'}, {'user_id': 'amy', 'timestamp': '2024-02-01 13:00:00', 'status': 'SUCcEss', 'country': 'US'}, {'user_id': 'amy', 'timestamp': '2024-02-02 13:00:00', 'status': 'SUCcEss', 'country': 'US'}, {'user_id': 'zoe', 'timestamp': '2024-02-03 12:00:00', 'status': 'SUCcEss', 'country': 'FR'}])

Output: ['zoe']

Example 3

Input: users_with_multi_country_successes([{'user_id': 'alice', 'timestamp': '2024-01-01 09:00:00', 'status': 'FAILED', 'country': 'US'}, {'user_id': 'alice', 'timestamp': '2024-01-02 09:00:00', 'status': 'SUCcEss', 'country': 'US'}, {'user_id': 'alice', 'timestamp': '2024-01-03 09:00:00', 'status': 'success', 'country': 'GB'}, {'user_id': 'bob', 'timestamp': '2024-01-04 09:00:00', 'status': 'SUCcEss', 'country': 'GB'}, {'user_id': 'bob', 'timestamp': '2024-01-05 09:00:00', 'status': 'SUCcEss', 'country': 'GB'}])

Output: []

Hints

Hint 1

Think about what you need to remember per user: not the number of logins, but which different countries they succeeded from.

Hint 2

Discard every event whose status is not exactly the required success string before collecting anything. Failures must never add a country.

Hint 3

Use a dictionary mapping each user to a set of countries, then keep users whose set has size at least 2 and sort the resulting IDs.