00:00

Problem Statement

Input/Output Examples

Visual Explanation

After the first element, each value is compared with the current min once. Because min always holds a real element, the method never returns a value that is not in the array.

Approaches

The loop shows the comparison explicitly. The stream version asks IntStream for its minimum.

Java Solutions

Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.

Complexity Analysis

Here n is the number of elements. Both approaches look at every element once and keep a single running minimum.

Interview Tips and Recommended Approach

  • Initialise from the array, not from a constant. min = arr[0] is always correct; a guessed starting value such as 1000 is not.
  • If you do use a constant, use Integer.MAX_VALUE. Then explain how you tell "the minimum is MAX_VALUE" apart from "the array was empty".
  • Handle the empty array explicitly. Accessing arr[0] on an empty array throws ArrayIndexOutOfBoundsException.
  • Offer a combined pass. If the interviewer asks for both the minimum and the maximum, track both in the same loop.

Recommended Approach: Single Loop

Why: it runs in one pass with constant memory and never depends on a guessed starting value. It is also the easiest version to extend to the minimum's index or to a combined min-max search.