Solve stock and banana problems
AppleMachine Learning Engineer · OnsiteLast updated
This question evaluates algorithmic problem-solving skills related to array processing, optimization under constraints, and reasoning about performance trade-offs by presenting a maximum single-transaction stock profit problem and a minimum banana-eating speed problem.
Solve both coding problems below.
-
Maximum single-transaction stock profit: Given an array
priceswhereprices[i]is the stock price on dayi, return the maximum profit obtainable by choosing one day to buy and a later day to sell. If no profit is possible, return0. -
Minimum banana-eating speed: Given an array
pileswherepiles[i]is the number of bananas in pilei, and an integerhrepresenting the total number of hours available, find the minimum integer eating speedksuch that a monkey can finish all piles withinhhours. In one hour, the monkey chooses one pile and eats up tokbananas from that pile.
For each problem, explain your algorithm and analyze time and space complexity.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.