Find Longest Call
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).
Example Input & Output
With one call, that duration is automatically the longest.
12 is the longest call duration in the list.
An empty call list has no valid duration to return.
Algorithm Flow
Solution Approach
Iterate through the array while tracking the maximum value seen so far.
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
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
endprogramComments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
