Codepath

Vantage Point

TIP102 Unit 1 Session 2 Standard (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: Arrays, Iteration, Conditionals

U-nderstand

Understand what the interviewer is asking for by using test cases and questions about the problem.

  • Established a set (2-3) of test cases to verify their own solution later.
  • Established a set (1-2) of edge cases to verify their solution handles complexities.
  • Have fully understood the problem and have no clarifying questions.
  • Have you verified any Time/Space Constraints for this problem?
  • The function highest_altitude() should take an integer array gain representing the net gain in altitude between consecutive points, and return the highest altitude reached during the trip.
HAPPY CASE
Input: [-5, 1, 5, 0, -7]
Expected Output: 1

Input: [-4, -3, -2, -1, 4, 3, 2]
Expected Output: 0

EDGE CASE
Input: [0, 0, 0, 0, 0]
Expected Output: 0

Input: [-1, -1, -1, -1, -1]
Expected Output: 0

P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Iterate through the gain array while maintaining the current altitude and updating the highest altitude encountered.

1. Initialize `current_altitude` to 0 (starting point altitude).
2. Initialize `highest_altitude` to 0.
3. Iterate through each value `g` in `gain`:
   a. Update `current_altitude` by adding `g`.
   b. Update `highest_altitude` if `current_altitude` is greater than `highest_altitude`.
4. Return `highest_altitude`

⚠️ Common Mistakes

  • Forgetting to initialize highest_altitude to 0.
  • Not correctly updating current_altitude during the iteration.

I-mplement

Implement the code to solve the algorithm.

def highest_altitude(gain):
    current_altitude = 0
    highest_altitude = 0
    
    for g in gain:
        current_altitude += g
        if current_altitude > highest_altitude:
            highest_altitude = current_altitude
    
    return highest_altitude
Fork me on GitHub