Grokking the Engineering Manager Coding Interview

0% completed

Find the Highest Altitude (easy)

Problem Statement

Examples

Try it yourself

Problem Statement

A bike rider is going on a ride. The road contains n + 1 points at different altitudes. The rider starts from point 0 at an altitude of 0.

Given an array of integers gain of length n, where gain[i] represents the net gain in altitude between points i and i + 1 for all (0 <= i < n), return the highest altitude of a point.

Examples

Example 1

  • Input: gain = [-5, 1, 5, 0, -7]
  • Expected Output: 1
  • Justification: The altitude changes are [-5, -4, 1, 1, -6], where 1 is the highest altitude reached.

Example 2

  • Input: gain = [4, -3, 2, -1, -2]
  • Expected Output: 4
  • Justification: The altitude changes are [4, 1, 3, 2, 0], where 4 is the highest altitude reached.

Example 3

  • Input: gain = [2, 2, -3, -1, 2, 1, -5]
  • Expected Output: 4
  • Justification: The altitude changes are [2, 4, 1, 0, 2, 3, -2], where 4 is the highest altitude reached.

Constraints:

  • n == gain.length
  • 1 <= n <= 100
  • -100 <= gain[i] <= 100

Try it yourself

Try solving this question here:

Python3
Python3

. . . .
Mark as Completed
C

cednice

· 3 years ago

If altitude array values are all negative, then initializing maxAltitude to zero will produce the incorrect answer.

Show 2 replies
T

tranlannhi

· 3 years ago

Would the solution with time O(logn) be better than O(n)? Could you please provide input for the following solution?

class Solution { largestAltitude(gain) { let maxAltitude = 0; let altChange = new Array(gain.length) altChange[0] = gain[0] for (let i=1; i< gain.length; i++){ altChange[i] = gain[i] + altChange[i-1] } altChange.sort((a,b) => b-a) maxAltitude = altChange[0] return maxAltitude; } }
Show 3 replies
A

anusha.inapakolla94

· 2 years ago

Hi Team,

May I know how many testcases each problem will be having as I could see 65 testcases are passed for one problem and 50 for another.

On This Page

Problem Statement

Examples

Try it yourself