Users Logging In Successfully From Several Countries
SQL & DataAuto-graded · 18 test casesLast 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 intimestamp: when the attempt happened (informational only, not needed for the answer)status: the outcome of the attemptcountry: 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
statusis 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_idexactly 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, andcountry, 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.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
This is a "filter, group, then filter on the aggregate" task.
- Walk through the events and skip any whose
statusis not exactly"SUCcEss". - For each remaining event, add its country to a set stored under the user's ID in a dictionary. Using a set means repeated logins from the same country collapse into one entry.
- After the pass, keep the users whose set contains at least two countries.
- Sort those user IDs and return them.
Why it is correct
- Filtering happens before any country is recorded, so a country that appears only in failed or differently-cased status events never counts toward a user. This matches the rule that only successful logins matter.
- A set per user holds exactly the distinct countries of that user's successful logins, so its size is the number of different countries.
- The threshold
>= 2matches "two or more different countries". - Each user is a dictionary key, so each appears at most once in the output. Sorting gives the required ordering, and an empty input or no qualifying users naturally gives an empty list.
Complexity
Let n be the number of events.
- Time: O(n) for the scan and set insertions on average, plus O(k log k) to sort the
kqualifying users. - Space: O(n) in the worst case for the per-user sets.
Reference solution
from typing import Dict, List
def users_with_multi_country_successes(login_events: List[Dict[str, str]]) -> List[str]:
countries_per_user = {}
for event in login_events:
if event["status"] == "SUCcEss":
countries_per_user.setdefault(event["user_id"], set()).add(event["country"])
return sorted(user for user, countries in countries_per_user.items() if len(countries) >= 2)