Find Largest Adjacent Stock Price Change
Company: Instacart
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving and data-manipulation skills, specifically date-based sorting, computing signed versus absolute adjacent differences, and correct handling of ordering and tie-breaking.
Constraints
- 0 <= len(records) <= 200000
- Each record contains a valid zero-padded date string in YYYY-MM-DD format and an integer price
- Dates are distinct
- 0 <= price <= 10^9
Examples
Input: [['2023-06-29', 110], ['2023-07-01', 112], ['2023-06-25', 90], ['2023-07-06', 105]]
Expected Output: [20, '2023-06-25', '2023-06-29']
Explanation: After sorting by date, the prices are 90 -> 110 -> 112 -> 105. The adjacent signed changes are 20, 2, and -7. The largest absolute change is 20, between 2023-06-25 and 2023-06-29.
Input: []
Expected Output: None
Explanation: There are no records, so no adjacent pair exists.
Input: [['2024-01-01', 150]]
Expected Output: None
Explanation: A single record does not form a pair.
Input: [['2023-01-03', 10], ['2023-01-01', 10], ['2023-01-02', 15]]
Expected Output: [5, '2023-01-01', '2023-01-02']
Explanation: After sorting, prices are 10 -> 15 -> 10. The adjacent changes are 5 and -5, both with absolute value 5. The earliest pair is returned.
Input: [['2023-01-03', 50], ['2023-01-01', 100], ['2023-01-02', 90], ['2023-01-04', 60]]
Expected Output: [-40, '2023-01-02', '2023-01-03']
Explanation: After sorting, prices are 100 -> 90 -> 50 -> 60. The adjacent changes are -10, -40, and 10. The largest absolute change is 40, from 2023-01-02 to 2023-01-03.
Hints
- Because the dates are in YYYY-MM-DD format, sorting the date strings lexicographically gives chronological order.
- Once the records are sorted, only neighboring records can form an adjacent pair. Track the largest absolute change, and on ties keep the first one you found.