Compute maximum later-earlier difference
Company: Citadel
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates array-processing and algorithmic optimization skills, testing reasoning about element relationships in sequences; it falls under the Coding & Algorithms domain and emphasizes practical application of linear-time algorithm design rather than purely conceptual proofs.
Constraints
- 1 <= len(output) <= 2 * 10^5
- -10^9 <= output[i] <= 10^9
Examples
Input: ([7, 1, 5, 3, 6, 4],)
Expected Output: 5
Explanation: The best choice is i = 1 (value 1) and j = 4 (value 6), giving 6 - 1 = 5.
Input: ([5, 4, 3, 2, 1],)
Expected Output: 0
Explanation: The array is strictly decreasing, so every later-earlier difference is non-positive.
Input: ([10],)
Expected Output: 0
Explanation: With only one element, no valid pair (i, j) exists.
Input: ([-4, -10, -3, 0],)
Expected Output: 10
Explanation: The best choice is -10 followed later by 0, for a gain of 10.
Input: ([2, 2, 2, 2],)
Expected Output: 0
Explanation: All values are equal, so the best difference is 0.
Hints
- As you scan from left to right, keep track of the smallest value seen so far.
- For each position j, the best earlier index i is the minimum element that appeared before j.