FastPrepSpeed Up a Slow Function
Problems/Speed Up a Slow Function

Speed Up a Slow Function

Citadel logoCitadel● EasyFULLTIMEOAPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Seen in 19 Citadel Securities accounts dated from August 2025 to November 2026, mostly from people applying for C++ engineering jobs. Nearly every one got this as a timed online test, and one person got it in a two-hour video interview. In each account the candidate is handed existing code and must decode it, tidy it and make it run far quicker; many describe the code as messy and hard to read. One account includes the whole starting code and a small program that times one call. That function takes an array of integers. At its heart it finds the biggest increase from an earlier number to a later one; it squares each difference and finally takes a square root.

We kept that behaviour and wrote the slow steps out plainly instead of showing the messy code. We added the value range from -20000 to 20000 so every squared difference fits in a 32-bit integer, and we ask for at least one value, because the original code does not work on an empty list. The original program fails any call slower than about 100 microseconds; here, long hidden tests take the place of that timer. The examples and the length limit are ours.

Our version is about 75% the same as the reported question.

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:

  1. Set best = 0.
  2. For every pair of positions i <= j: if values[j] >= values[i], square the difference values[j] - values[i], and if the square is larger than best, store it in best.
  3. Count r = 0, 1, 2, ... upward and stop at the first r with r * r >= best.
  4. 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[]) → int

Examples

Example 1

values = [4,9,2,6,8,3]return = 6

The 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 = 0

Every 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 = 10

The 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
All Citadel interview questionsSee Citadel hiring insights
Saved
class Solution {
    public int rootNode(int[] values) {
      // Write your code here.
    }
}
Checking account…