Code Logo

Find Longest Call

Published at19 Apr 2026
Easy 0 views
Like0

This problem is based on checking call history. You have a list of call durations, and you want to know which call lasted the longest.

The answer is simply the largest number in the list. You do not need to sort the whole list. You only need to keep track of the biggest duration you have seen so far.

For example, if the durations are [4, 12, 7, 9], the answer is 12. That is the longest call in the list.

If the list is empty, return -1 because there is no call duration to report.

Example Input & Output

Example 1
Input
calls = [5]
Output
5
Explanation

With one call, that duration is automatically the longest.

Example 2
Input
calls = [4, 12, 7, 9]
Output
12
Explanation

12 is the longest call duration in the list.

Example 3
Input
calls = []
Output
-1
Explanation

An empty call list has no valid duration to return.

Algorithm Flow

Recommendation Algorithm Flow for Find Longest Call
Recommendation Algorithm Flow for Find Longest Call

Solution Approach

A good way to solve this is to keep one variable for the current longest duration.

First, handle the empty-list case. If there are no calls, return -1.

If the list has values, store the first call duration in a variable like longest. That gives you a starting answer.

Then move through the remaining durations and compare each one with longest.

The update rule is:

if calls[i] > longest then longest <- calls[i] endif

Whenever you find a bigger duration, replace the old value.

After the loop ends, longest is the largest duration in the list, so that is the answer you print.

Best Answers

Solutions Coming Soon

Verified best solutions for this Challenge are still being analyzed and will be available soon.