← Sliding Window Quiz: all 8 questions
Sliding Window Quiz · Question 2 of 8
A fixed window of size k slides one step right. How is the new sum computed in O(1)?
A fixed window of size k slides one step right. How is the new sum computed in O(1)?
- A.Re-add all k elements
- B.Add the new element, subtract the one that left
- C.Multiply the old sum by k
- D.Sort the window
Answer & explanation
Correct answer: Add the new element, subtract the one that left
Only two elements change, so update the sum incrementally.
Can you get the rest right without peeking?
Take the timed Sliding Window Quiz quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →