Skip to main content
All Questions

Find the maximum coins collectable from k consecutive bags given non-overlapping coin segments.

Unlock detailed company stats for this questionUpgrade

You are given n segments, each represented as [l, r, c], meaning every bag from position l to r contains exactly c coins. The segments are guaranteed not to overlap. You are also given an integer k. Find the maximum number of coins that can be collected from k consecutive bags. All other bags have zero coins.

Constraints: 1 <= n <= 10^5; 1 <= l <= r <= 10^9; 1 <= c <= 10^9; 1 <= k <= 10^9. Because coordinates go up to 10^9, a direct array-based approach is impossible.

Example: segments = [[1,4,2],[6,6,5],[7,7,7],[9,10,1]], k = 5. Bags at positions 1..10 hold 2 2 2 2 0 5 7 0 1 1. Window sums: [1-5]=8, [2-6]=11, [3-7]=16, [4-8]=14, [5-9]=13, [6-10]=14. Answer: 16.

Format · Online assessment.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.