Hone logo
Hone
Problems

Linear Scan for Maximum Value

Linear scan, also known as a sequential search, is a fundamental algorithm used to find the maximum value within a collection of numbers. This challenge asks you to implement a linear scan function in Go to efficiently identify the largest number in a given slice of integers. Understanding and implementing linear scan is crucial for building more complex algorithms and data processing pipelines.

Problem Description

You are tasked with creating a Go function called FindMax that takes a slice of integers ([]int) as input and returns the maximum value found within that slice. The function should iterate through each element of the slice, comparing it to the current maximum value. If an element is greater than the current maximum, the maximum value is updated.

Key Requirements:

  • The function must handle empty slices gracefully.
  • The function must correctly identify the maximum value even if the slice contains negative numbers.
  • The function should be efficient for reasonably sized slices.

Expected Behavior:

The FindMax function should return the largest integer present in the input slice. If the slice is empty, it should return a sensible default value (e.g., the smallest possible integer).

Edge Cases to Consider:

  • Empty Slice: What should the function return if the input slice is empty?
  • Slice with all negative numbers: Ensure the function correctly identifies the least negative number as the maximum.
  • Slice with duplicate maximum values: The function should return any one of the maximum values.
  • Slice with a single element: The function should return that single element.

Examples

Example 1:

Input: [1, 5, 2, 8, 3]
Output: 8
Explanation: The function iterates through the slice. 8 is the largest value, so it's returned.

Example 2:

Input: [-5, -2, -8, -1]
Output: -1
Explanation: The function iterates through the slice. -1 is the largest (least negative) value, so it's returned.

Example 3:

Input: []
Output: -9223372036854775808  // Minimum int64 value
Explanation: The slice is empty. The function returns the minimum possible int64 value to indicate no maximum was found.

Example 4:

Input: [7]
Output: 7
Explanation: The slice contains only one element, which is the maximum.

Constraints

  • The input slice can contain any number of integers (including zero).
  • The input slice can be empty.
  • The integers in the slice can be positive, negative, or zero.
  • The function should have a time complexity of O(n), where n is the number of elements in the slice.
  • The function should not modify the input slice.

Notes

  • Consider initializing the max variable to the smallest possible integer value to handle cases where all numbers in the slice are negative. The math.MinInt64 constant from the math package can be helpful for this.
  • Think about how to handle the edge case of an empty slice. Returning a default value is a common approach.
  • The focus is on clarity and correctness. While performance is a consideration, prioritize a readable and understandable solution.
Loading editor...
go