Question bank › Prefix Sum
Dsa Prefix Sum Easy

Cumulative Distance

You have a list of distances covered by a vehicle over 'n' trips. Each trip's distance is an integer, and you need to determine the cumulative distance over specific intervals of trips. Write a function that reads the number of trips and the respective distances, followed by a number of queries. Each query consists of two integers representing the start and end of the trip intervals. Output the total distance covered for each query. Input: The first line contains an integer n (1 ≤ n ≤ 10^5), the number of trips. The second line contains n space-separated integers representing the distances for each trip. The third line contains an integer q (1 ≤ q ≤ 10^5), the number of queries. The following q lines each contain two space-separated integers, start and end (1 ≤ start ≤ end ≤ n). Output: For each query, output the total cumulative distance for the specified interval. Example: Input: 4 10 20 30 40 2 1 2 2 4 Output: 30 90

Key concepts

prefix_sumrange_query

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of Praxari's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.