Fancy Sequence - Problem

Design and implement a Fancy Sequence data structure that supports dynamic operations on a sequence of integers. This sequence allows you to efficiently append new values, apply bulk transformations (add or multiply all elements), and query values at specific indices.

The challenge is to handle potentially millions of operations efficiently while maintaining the correct sequence state. All values should be returned modulo 109 + 7 to prevent integer overflow.

Operations to implement:

  • append(val) - Add integer val to the end of the sequence
  • addAll(inc) - Add inc to every existing element in the sequence
  • multAll(m) - Multiply every existing element in the sequence by m
  • getIndex(idx) - Return the current value at index idx (0-indexed), or -1 if index is out of bounds

Key insight: Naive approaches will time out on large inputs due to the bulk operations affecting all elements.

Input & Output

example_1.py โ€” Basic Operations
$ Input: fancy = Fancy() fancy.append(2) fancy.addAll(3) fancy.append(7) fancy.multAll(2) print(fancy.getIndex(0)) print(fancy.getIndex(1))
โ€บ Output: 10 14
๐Ÿ’ก Note: First append 2, then add 3 to all (making it 5), append 7, then multiply all by 2. Index 0: (2+3)*2 = 10, Index 1: 7*2 = 14
example_2.py โ€” Out of bounds
$ Input: fancy = Fancy() fancy.append(2) print(fancy.getIndex(0)) print(fancy.getIndex(1))
โ€บ Output: 2 -1
๐Ÿ’ก Note: Index 1 is out of bounds since we only have one element, so return -1
example_3.py โ€” Multiple transformations
$ Input: fancy = Fancy() fancy.append(0) fancy.addAll(5) fancy.multAll(3) fancy.append(10) fancy.multAll(2) print(fancy.getIndex(0)) print(fancy.getIndex(1))
โ€บ Output: 30 20
๐Ÿ’ก Note: Index 0: ((0+5)*3)*2 = 30. Index 1: 10*2 = 20 (only affected by final multAll)

Constraints

  • 1 โ‰ค val, inc, m โ‰ค 100
  • 0 โ‰ค idx โ‰ค 105
  • At most 105 calls will be made to append, addAll, multAll, and getIndex
  • All values must be returned modulo 109 + 7

Visualization

Tap to expand
๐ŸŽญ Fancy Sequence: Transformation Theater๐ŸŽฌ Transformation Stage (Global State)Add Factor+3Mult Factorร—2OperationsO(1)2Actor #1(Time: 0)7Actor #2(Time: 1)5Actor #3(Time: 2)Lazy ApplyOn Demand๐ŸŽฏ getIndex(0) ResultApply all transformations since Time 0:(2 + 3) ร— 2 = 10
Understanding the Visualization
1
Record Original Values
Store each number with a timestamp of when it joined the sequence
2
Track Global Transformations
Instead of applying operations immediately, record them as global transformation factors
3
Apply on Demand
When getIndex is called, calculate the final value by applying all transformations that occurred after the element was added
4
Use Modular Arithmetic
Handle large numbers and division using modular inverse to ensure correctness
Key Takeaway
๐ŸŽฏ Key Insight: By deferring the application of transformations until they're actually needed, we convert expensive O(n) bulk operations into O(1) bookkeeping, making the data structure highly efficient for large sequences with frequent transformations.
Asked in
Google 25 Amazon 18 Meta 12 Microsoft 8
27.6K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen