← 返回 instacart 的题目列表Karat Coding: Max Adjacent Stock Price Change
类型:qbank
Karat coding follow-up using a list of stock price records. Convert records into a sortable sequence, sort by date, compare adjacent trading records, and return the largest adjacent price change with the two dates that produced it.
Requirements
Input is a list of stock record objects. Each record contains a date and a price.
Convert or inspect the record class enough to extract date and price.
Sort records by date ascending before comparing prices.
Compare adjacent records after sorting and compute the price change from the earlier date to the later date.
Return the largest adjacent change together with the two dates that produced it. The returned shape in the shown example is [change, start_date, end_date].
Examples
Input records:
Price: 110 112 90 105
Date: 2023-06-29 2023-07-01 2023-06-25 2023-07-06
After sorting by date:
Date: 2023-06-25 -> 2023-06-29 -> 2023-07-01 -> 2023-07-06
Price: 90 -> 110 -> 112 -> 105
Change: +20 -> +2 -> -7
Expected return:
[20, "2023-06-25", "2023-06-29"]
Notes
The round is time-compressed. Read the existing record class first, then write the sorting and adjacent scan directly.
The example identifies +20 as the biggest change. Clarify whether the comparator is largest signed increase or largest absolute price movement if negative drops can dominate.
The same Karat screen can include an initial bug-fix task where several missing/null branches should return None; run the tests first and follow the failing lines.
Do not spend the round building a complex financial model. The intended task is class inspection, sorting by date, and a linear adjacent scan.
Preparation
Practice extracting fields from small classes or dictionaries before writing the algorithm.
Drill sorting records by ISO date string or parsed date, then scanning adjacent pairs in one pass.
Prepare the clarification sentence for signed vs absolute price change before coding.