Frontend School

Portfolio Multiplier Tracker

DSA
medium

Portfolio Multiplier Tracker

Medium
Segment Tree
Lazy Propagation
Range Query
Range Update

Overview

In financial modeling, accurately tracking the cumulative effect of various market factors on portfolio returns is crucial. This problem simulates a system where daily profit/loss multipliers are applied and queried over specific periods. You'll build a data structure to efficiently handle updates affecting a range of days and queries asking for the total multiplicative effect over another range.

What you need to do / Task

Implement a function simulatePortfolioMultipliers that takes an initial number of days N and a list of operations. Each operation is either an update or a query.

  1. Initialize: Create a data structure representing N days, where each day initially has a multiplier of 1.0.
  2. Apply Multiplier Update: Given a startDay, endDay, and percentageChange, update the multiplier for all days i from startDay to endDay (inclusive). The new multiplier for each day i will be currentMultiplier_i * (1 + percentageChange / 100).
  3. Query Cumulative Multiplier: Given a queryStartDay and queryEndDay, return the product of multipliers for all days i from queryStartDay to queryEndDay (inclusive).

Input & output

simulatePortfolioMultipliers(numDays: number, operations: Operation[]): number[]

  • numDays: An integer representing the total number of days (indexed 0 to numDays - 1).
  • operations: An array of Operation objects. Each Operation has a type field ("update" or "query").
    • If type is "update":
      • startDay: number, the start index of the day range (inclusive).
      • endDay: number, the end index of the day range (inclusive).
      • percentageChange: number, the percentage change (e.g., 5 for +5%, -10 for -10%).
    • If type is "query":
      • startDay: number, the start index of the day range (inclusive).
      • endDay: number, the end index of the day range (inclusive).
  • Returns: number[], an array of results for each "query" operation, in the order they appear in operations.

Clarifications

  • Day indices are 0-based.
  • percentageChange can be positive or negative. A percentageChange of P means multiplying by (1 + P/100).
  • The results of cumulative multipliers might be floating-point numbers. Precision up to 5-6 decimal places is expected.

Examples

1

Example 1

Input:

numDays = 5, operations = [ { type: "update", startDay: 0, endDay: 2, percentageChange: 10 }, { type: "query", startDay: 0, endDay: 4 }, { type: "update", startDay: 1, endDay: 3, percentageChange: -5 }, { type: "query", startDay: 0, endDay: 4 } ]

Output:

[1.331,1.139989375]

Note:

Initial multipliers: [1.0, 1.0, 1.0, 1.0, 1.0]

  1. update(0, 2, 10): Multipliers for days 0, 1, 2 become *1.1. Array: [1.1, 1.1, 1.1, 1.0, 1.0]
  2. query(0, 4): Product 1.1 * 1.1 * 1.1 * 1.0 * 1.0 = 1.331
  3. update(1, 3, -5): Multipliers for days 1, 2, 3 become *0.95. Array: [1.1, 1.1*0.95, 1.1*0.95, 1.0*0.95, 1.0] => [1.1, 1.045, 1.045, 0.95, 1.0]
  4. query(0, 4): Product 1.1 * 1.045 * 1.045 * 0.95 * 1.0 = 1.139989375
2

Example 2

Input:

numDays = 1, operations = [ { type: "update", startDay: 0, endDay: 0, percentageChange: 20 }, { type: "query", startDay: 0, endDay: 0 }, { type: "update", startDay: 0, endDay: 0, percentageChange: -10 }, { type: "query", startDay: 0, endDay: 0 } ]

Output:

[1.2,1.08]

Note:

Initial multipliers: [1.0]

  1. update(0, 0, 20): Multiplier for day 0 becomes *1.2. Array: [1.2]
  2. query(0, 0): Product 1.2
  3. update(0, 0, -10): Multiplier for day 0 becomes *0.9. Array: [1.2 * 0.9 = 1.08]
  4. query(0, 0): Product 1.08
3

Example 3

Input:

numDays = 3, operations = [ { type: "query", startDay: 0, endDay: 2 }, { type: "update", startDay: 0, endDay: 0, percentageChange: 50 }, { type: "update", startDay: 1, endDay: 1, percentageChange: -20 }, { type: "query", startDay: 0, endDay: 1 }, { type: "update", startDay: 2, endDay: 2, percentageChange: 100 }, { type: "query", startDay: 0, endDay: 2 } ]

Output:

[1.0,1.2,2.4]

Note:

Initial multipliers: [1.0, 1.0, 1.0]

  1. query(0, 2): Product 1.0 * 1.0 * 1.0 = 1.0
  2. update(0, 0, 50): Day 0 becomes *1.5. Array: [1.5, 1.0, 1.0]
  3. update(1, 1, -20): Day 1 becomes *0.8. Array: [1.5, 0.8, 1.0]
  4. query(0, 1): Product 1.5 * 0.8 = 1.2
  5. update(2, 2, 100): Day 2 becomes *2.0. Array: [1.5, 0.8, 2.0]
  6. query(0, 2): Product 1.5 * 0.8 * 2.0 = 2.4

Constraints

Constraints & assumptions

  • 1 <= numDays <= 10^5
  • 1 <= operations.length <= 10^5
  • 0 <= startDay <= endDay < numDays
  • -99 <= percentageChange <= 1000 (This ensures multipliers remain positive, 1 - 99/100 = 0.01 minimum)
  • Time complexity for each update and query operation should be O(log N).
  • Space complexity should be O(N).

What interviewers evaluate

  • Correct implementation of Segment Tree with lazy propagation for range product.
  • Handling of floating-point arithmetic and precision considerations.
  • Efficiency of update and query operations (logarithmic time complexity).
  • Code structure, clarity, and handling of edge cases (e.g., single-day ranges).

Loading editor...