PracHub
QuestionsPremiumLearningGuidesCheatsheetNEW

Quick Overview

This question evaluates array-processing and algorithmic optimization skills, testing reasoning about element relationships in sequences; it falls under the Coding & Algorithms domain and emphasizes practical application of linear-time algorithm design rather than purely conceptual proofs.

  • medium
  • Citadel
  • Coding & Algorithms
  • Software Engineer

Compute maximum later-earlier difference

Company: Citadel

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

You are given an integer array `output` (length `n ≥ 1`). Define the **gain** of choosing indices `i < j` as `output[j] - output[i]`. Return the **maximum gain** over all valid pairs `(i, j)`. If no pair yields a positive gain, return `0`. ### Input - `output`: `vector<int>` ### Output - An integer: the maximum possible `output[j] - output[i]` for `i < j`, or `0` if all differences are non-positive. ### Examples - `output = [7, 1, 5, 3, 6, 4]` → `5` (buy at `1`, sell at `6`) - `output = [5, 4, 3, 2, 1]` → `0` ### Constraints (typical) - `1 ≤ n ≤ 2e5` - `-1e9 ≤ output[i] ≤ 1e9` Implement `int root_node(vector<int> output)` efficiently (time better than O(n²)).

Quick Answer: This question evaluates array-processing and algorithmic optimization skills, testing reasoning about element relationships in sequences; it falls under the Coding & Algorithms domain and emphasizes practical application of linear-time algorithm design rather than purely conceptual proofs.

You are given an integer array output. Choose two indices i and j such that i < j. The gain of this choice is output[j] - output[i]. Return the maximum possible gain over all valid pairs. If every possible gain is less than or equal to 0, return 0. This is the same idea as buying at an earlier day and selling at a later day for maximum profit.

Constraints

  • 1 <= len(output) <= 2 * 10^5
  • -10^9 <= output[i] <= 10^9

Examples

Input: ([7, 1, 5, 3, 6, 4],)

Expected Output: 5

Explanation: The best choice is i = 1 (value 1) and j = 4 (value 6), giving 6 - 1 = 5.

Input: ([5, 4, 3, 2, 1],)

Expected Output: 0

Explanation: The array is strictly decreasing, so every later-earlier difference is non-positive.

Input: ([10],)

Expected Output: 0

Explanation: With only one element, no valid pair (i, j) exists.

Input: ([-4, -10, -3, 0],)

Expected Output: 10

Explanation: The best choice is -10 followed later by 0, for a gain of 10.

Input: ([2, 2, 2, 2],)

Expected Output: 0

Explanation: All values are equal, so the best difference is 0.

Hints

  1. As you scan from left to right, keep track of the smallest value seen so far.
  2. For each position j, the best earlier index i is the minimum element that appeared before j.
Last updated: May 6, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement a single-producer multi-consumer ring buffer - Citadel (medium)
  • Sort a Nearly Sorted Array - Citadel (hard)
  • Compute BBO and NBBO from order data - Citadel (medium)
  • Design dynamic weighted random sampling with updates - Citadel (medium)
  • Implement LRU/LFU cache with custom eviction - Citadel (easy)