Speed Up a Slow Function
Updated
Problem statement
You are given a function rootNode(values) that always returns the right answer but is far too slow on long inputs. Work out what it computes, then write a version that returns exactly the same value and runs fast.
The slow version does this:
- Set
best = 0. - For every pair of positions
i <= j: ifvalues[j] >= values[i], square the differencevalues[j] - values[i], and if the square is larger thanbest, store it inbest. - Count
r = 0, 1, 2, ...upward and stop at the firstrwithr * r >= best. - Return
r.
Return the same value as the slow version. It checks every pair, so its running time grows with n2 for n values. The tests include arrays long enough that checking every pair is too slow, so aim for one pass over the array.
Function
rootNode(values: int[]) → intExamples
Example 1
values = [4,9,2,6,8,3]return = 6The biggest rise from an earlier value to a later one goes from 2 to 8, which is 6. The slow version keeps best = 36 and then counts up to r = 6.
Example 2
values = [10,7,5,1]return = 0Every later value is smaller than every earlier one, so only pairs with i = j pass the check, and each gives 0. best stays 0, so r = 0.
Example 3
values = [-3,5,-8,2]return = 10The rise from -8 to 2 is 10, bigger than the rise from -3 to 5, which is 8. So best = 100 and r = 10.
Constraints
1 <= values.length <= 105-20000 <= values[i] <= 20000
More Citadel Online Assessment questions
- Possible MEX Values After One Optional IncrementOA · Seen Oct 2026
- Busy IntersectionOA · Seen Sep 2026
- Shortest Path in an Unweighted GraphOA · Seen Sep 2026
- Maximize Workday EarningsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Changes for Palindromic BlocksOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Minimum Image Processing CostOA · Seen Jul 2026
- Coprime Pick with Change CostOA · Seen Jul 2026
- Process SchedulingOA · Seen Mar 2026
- Social Media SuggestionsOA · Seen May 2025
- Best Sum Downward Tree PathOA · Seen May 2025
Similar questions at other companies
- Best Time to Buy and Sell StockMeta · PHONE SCREENMeta interview questions
- Best Time to Buy and Sell StockSwiggy · PHONE SCREEN
- Maximum Income from Asset TradingJefferies · OA
- Best Time to Buy and Sell StockFlexTrade · OAFlexTrade interview questions
- Suffix Maximum Frequency QueriesMicrosoft · PHONE SCREENMicrosoft interview questions
- Count Sawtooth SubarraysHudson River Trading · OAHudson River Trading interview questions
- Best Time to Buy and Sell StockOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Best Time to Buy and Sell StockJP Morgan Chase · PHONE SCREENJP Morgan Chase interview questions
- Maximum Total of Nonadjacent RatingsMorgan Stanley · OAMorgan Stanley interview questions
- Count Sawtooth SubarraysTikTok · OATikTok interview questions
Citadel interview experiences
- Citadel Securities Quant Researcher Intern Loop: 1 Experience
- Citadel Securities Research Engineer Interview: 1 Experience
- Citadel Securities SWE Intern First Round: 2 Experiences
- Citadel Securities Experienced SWE Interviews: 1 Experience
- Citadel SWE Intern Later Rounds and Superday: 13 Experiences
- Citadel SWE Intern First-Round Questions: 20 Experiences