Skip to main content

Find Longest Calls - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase6 min read
Practice this problem

Problem Statement

Table: Contacts

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| first_name  | varchar |
| last_name   | varchar |
+-------------+---------+
id is the primary key (column with unique values) of this table.
id is a foreign key (reference column) to Calls table.
Each row of this table contains id, first_name, and last_name.

Table: Calls

+-------------+------+
| Column Name | Type |
+-------------+------+
| contact_id  | int  |
| type        | enum |
| duration    | int  |
+-------------+------+
(contact_id, type, duration) is the primary key (column with unique values) of this table.
type is an ENUM (category) type of ('incoming', 'outgoing').
Each row of this table contains information about calls, comprising of contact_id, type, and duration in seconds.

Write a solution to find the three longest incoming and outgoing calls.

Return the result table ordered by type, duration, and first_name in descending order and duration must be formatted as HH:MM:SS.

The result format is in the following example.

 

Example 1:

Input:

Contacts table:

+----+------------+-----------+
| id | first_name | last_name |
+----+------------+-----------+
| 1  | John       | Doe       |
| 2  | Jane       | Smith     |
| 3  | Alice      | Johnson   |
| 4  | Michael    | Brown     |
| 5  | Emily      | Davis     |
+----+------------+-----------+        

Calls table:

+------------+----------+----------+
| contact_id | type     | duration |
+------------+----------+----------+
| 1          | incoming | 120      |
| 1          | outgoing | 180      |
| 2          | incoming | 300      |
| 2          | outgoing | 240      |
| 3          | incoming | 150      |
| 3          | outgoing | 360      |
| 4          | incoming | 420      |
| 4          | outgoing | 200      |
| 5          | incoming | 180      |
| 5          | outgoing | 280      |
+------------+----------+----------+
        

Output:

+-----------+----------+-------------------+
| first_name| type     | duration_formatted|
+-----------+----------+-------------------+
| Alice     | outgoing | 00:06:00          |
| Emily     | outgoing | 00:04:40          |
| Jane      | outgoing | 00:04:00          |
| Michael   | incoming | 00:07:00          |
| Jane      | incoming | 00:05:00          |
| Emily     | incoming | 00:03:00          |
+-----------+----------+-------------------+
        

Explanation:

  • Alice had an outgoing call lasting 6 minutes.
  • Emily had an outgoing call lasting 4 minutes and 40 seconds.
  • Jane had an outgoing call lasting 4 minutes.
  • Michael had an incoming call lasting 7 minutes.
  • Jane had an incoming call lasting 5 minutes.
  • Emily had an incoming call lasting 3 minutes.

Note: Output table is sorted by type, duration, and first_name in descending order.

Approach Overview

Problem Overview: The task asks you to identify the longest phone calls recorded in a call log table. Each record represents a call between two users with a duration. The goal is to determine which calls have the maximum duration under the required grouping conditions (such as per caller or user pair) and return the relevant rows.

Approach 1: Aggregation + Join (O(n log n) time, O(n) space)

A common starting point is to compute the maximum call duration using GROUP BY. For example, group rows by the entity you care about (such as caller_id or a caller–receiver pair) and compute MAX(duration). That subquery gives the longest duration per group. Then perform an equi-join between the original Calls table and the aggregated result to retrieve the full rows that match the maximum duration. This approach works well when the grouping logic is simple and you only need rows matching the computed maximum.

Approach 2: Equi-Join + Window Function (O(n log n) time, O(n) space)

The cleaner and usually preferred solution uses a window function such as ROW_NUMBER() or RANK(). First join any required tables using an equi-join (for example, joining call records with user metadata if needed). Then apply a window function with PARTITION BY to define the group (such as per caller or per call pair) and ORDER BY duration DESC so the longest call appears first in each partition. Assign row numbers and filter for row_number = 1. This directly returns the longest call in each group without needing a separate aggregation join.

Window functions are powerful because they operate on ordered partitions of data. Instead of collapsing rows like GROUP BY, they keep all rows visible while still computing rankings or aggregates. That makes them ideal for "top‑N per group" problems commonly seen in SQL interview questions and database analytics.

These techniques appear frequently in database interview problems involving SQL, especially when identifying maximum values within groups or ranking records. Understanding how ROW_NUMBER, RANK, and DENSE_RANK behave within partitions is key for problems involving window functions and relational joins like database equi-joins.

Recommended for interviews: The window function approach with ROW_NUMBER() is what most interviewers expect. It is concise, expressive, and scales well for "top record per group" queries. Showing the aggregation + join method first demonstrates understanding of relational operations, but the window function solution signals stronger SQL fluency.

Solution

We can use equi-join to connect the two tables, and then use the window function RANK() to calculate the ranking of each type of phone. Finally, we just need to filter out the top three phones.

Code

MySQL

Python

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Aggregation + JoinO(n log n)O(n)Useful when computing MAX/MIN per group and then retrieving matching rows
Equi-Join + Window Function (ROW_NUMBER)O(n log n)O(n)Best for top‑N per group queries and modern SQL interview problems

Video Solution

Leetcode MEDIUM 3124 - Find Longest Calls RANKING in SQL - Explained by Everyday Data ScienceEveryday Data Science547 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is Find Longest Calls easy or hard?
Find Longest Calls is generally considered a medium difficulty SQL problem. The challenge lies in correctly grouping rows and extracting the maximum duration record using joins or window functions.
Find Longest Calls Python/Java solution
When solved outside SQL, the logic typically involves grouping records by key and tracking the maximum duration using dictionaries or hash maps. Python solutions often use pandas or collections, while Java implementations rely on HashMap and custom sorting.
How to solve Find Longest Calls in O(n)?
Pure O(n) solutions are uncommon in SQL because ranking or grouping operations usually require sorting. The closest practical approach uses ROW_NUMBER() with PARTITION BY, which performs efficiently in O(n log n) time. Proper indexing on grouping columns can significantly improve real-world performance.
What is the best approach for Find Longest Calls?
The most efficient and readable solution uses a window function such as ROW_NUMBER() or RANK() with PARTITION BY and ORDER BY duration DESC. This ranks calls inside each group and allows you to filter the longest call directly. The approach typically runs in O(n log n) time due to sorting within partitions and uses O(n) space.
Is Find Longest Calls asked at Google/Amazon/Meta?
Database ranking and top‑per‑group problems appear frequently in SQL interview rounds at companies like Amazon, Meta, and Google. Variations of this question test knowledge of window functions, joins, and grouping logic in relational databases.
What data structure is used in Find Longest Calls?
In SQL-based problems, the main structures are relational tables combined with logical partitions created by window functions. Internally, the database engine may use sorting structures and temporary buffers to rank rows within each partition.
What is the time complexity of Find Longest Calls?
Most SQL solutions run in O(n log n) time because the database engine sorts rows when applying ORDER BY in window functions or grouping operations. Space complexity is generally O(n) for intermediate query results or partitions maintained by the query planner.

Ready to solve this problem?

Practice Find Longest Calls with our built-in code editor and test cases.

Practice on FleetCode