Top 3 Most Frequent API Calls

Quick Overview

Given a list of API endpoint call logs, return the three most frequently called endpoints, breaking frequency ties in alphabetical order. It tests hash map counting, sorting with a composite key, and handling inputs with fewer than three unique endpoints.

Top 3 Most Frequent API Calls

Company: Roblox

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 ```text 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"`. ```text 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`. ```text 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).

Overview: Given a list of API endpoint call logs, return the three most frequently called endpoints, breaking frequency ties in alphabetical order. It tests hash map counting, sorting with a composite key, and handling inputs with fewer than three unique endpoints.

Roblox logo
Roblox
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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).