Code Logo

Find Longest Call

Published at19 Apr 2026
Searching & Sorting Easy 8 views
Like0

Given an array of call durations in minutes, find the longest call duration. Return the maximum value from the list. If the array is empty, return 0.

For example, the longest call from [5, 12, 3, 8, 15] is 15 minutes. From [10] it is 10. From [0, 0, 0] it is 0. An empty list returns 0.

Finding the maximum value is a fundamental algorithmic pattern used across many domains: longest customer wait time, highest temperature reading, largest transaction amount, or maximum file size. The pattern teaches you to track a running maximum by comparing each new value against the best found so far.

The solution initializes a max variable with the first element, then iterates through the remaining values. If a value exceeds the current max, update it. After the loop, return max. This runs in O(n) time with O(1) space, which is optimal since every element must be examined.

Edge cases include an empty array (return 0), a single-element array (return that element), all equal values (return that value), and negative durations (handled naturally by the comparison).

Learn about our pseudocode specification
Guide

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

Solution Approach

Iterate through the array while tracking the maximum value seen so far.

function longestCall(durations)
  if durations is empty then return 0
  max = durations[0]
  for each d in durations
    if d > max then max = d
  return max

Handle the empty case first. Initialize max to the first element. Loop through each duration; if the current duration is greater than max, update max. After the loop, return the maximum found.

Time complexity is O(n), space complexity is O(1).

Best Answers

Pseudocode - Approach 1
program find_longest_call
dictionary
   calls: array[1..100] of integer
   longest, i, n: integer
algorithm
   input(calls)
   n <- calls.length
   if n = 0 then
      output(-1)
   else
      longest <- calls[0]
      for i <- 1 to n - 1 do
         if calls[i] > longest then
            longest <- calls[i]
         endif
      endfor
      output(longest)
   endif
endprogram