Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.
You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i and i + 1 for all (0 <= i < n). Return the highest altitude of a point.
Example 1:
Input: gain = [-5,1,5,0,-7] Output: 1 Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
Example 2:
Input: gain = [-4,-3,-2,-1,4,3,2] Output: 0 Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
Constraints:
n == gain.length1 <= n <= 100-100 <= gain[i] <= 100Problem summary: There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0. You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i and i + 1 for all (0 <= i < n). Return the highest altitude of a point.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[-5,1,5,0,-7]
[-4,-3,-2,-1,4,3,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1732: Find the Highest Altitude
class Solution {
public int largestAltitude(int[] gain) {
int ans = 0, h = 0;
for (int v : gain) {
h += v;
ans = Math.max(ans, h);
}
return ans;
}
}
// Accepted solution for LeetCode #1732: Find the Highest Altitude
func largestAltitude(gain []int) (ans int) {
h := 0
for _, v := range gain {
h += v
if ans < h {
ans = h
}
}
return
}
# Accepted solution for LeetCode #1732: Find the Highest Altitude
class Solution:
def largestAltitude(self, gain: List[int]) -> int:
return max(accumulate(gain, initial=0))
// Accepted solution for LeetCode #1732: Find the Highest Altitude
impl Solution {
pub fn largest_altitude(gain: Vec<i32>) -> i32 {
let mut ans = 0;
let mut h = 0;
for v in gain.iter() {
h += v;
ans = ans.max(h);
}
ans
}
}
// Accepted solution for LeetCode #1732: Find the Highest Altitude
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1732: Find the Highest Altitude
// class Solution {
// public int largestAltitude(int[] gain) {
// int ans = 0, h = 0;
// for (int v : gain) {
// h += v;
// ans = Math.max(ans, h);
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.