You are given a list of API call logs, logs. Each log entry is a string naming the API endpoint that was called, such as "/api/users" or "/api/orders". Return the top 3 most frequently called endpoints.
If there is a tie in frequency, break the tie by alphabetical order: the lexicographically smaller endpoint comes first. If there are fewer than 3 unique endpoints, return all unique endpoints sorted by the same criteria.
Function Signature
def top_three_api_calls(logs: list[str]) -> list[str]:
...
Rules
-
Return at most 3 endpoints, ordered by call count from highest to lowest.
-
Endpoints with the same count are ordered by plain string comparison in ascending order (character by character on character codes, which is how Python compares strings).
-
Each endpoint appears at most once in the output.
Constraints
-
1 <= len(logs) <= 10^5
-
1 <= len(logs[i]) <= 100
-
logs[i]
consists of lowercase English letters, digits,
/
,
-
, and
_
.
Examples
Input: logs = ["/api/users","/api/orders","/api/users","/api/products","/api/orders","/api/users","/api/products","/api/orders","/api/health"]
Output: ["/api/orders","/api/users","/api/products"]
"/api/users" and "/api/orders" each appear 3 times, "/api/products" appears 2 times and "/api/health" appears once. The two endpoints tied at 3 are ordered alphabetically, so "/api/orders" comes before "/api/users".
Input: logs = ["/api/login","/api/login","/api/logout","/api/logout","/api/dashboard","/api/dashboard","/api/settings"]
Output: ["/api/dashboard","/api/login","/api/logout"]
"/api/login", "/api/logout" and "/api/dashboard" each appear 2 times, while "/api/settings" appears once. All three tied endpoints are returned alphabetically; "/api/login" sorts before "/api/logout" because i comes before o.
Input: logs = ["/api/health","/api/health","/api/auth"]
Output: ["/api/health","/api/auth"]
There are only 2 unique endpoints, so both are returned: "/api/health" (2 calls) before "/api/auth" (1 call).