Find the pivot index of a sales list
Python data engineering interview problem. Difficulty: intermediate. Pattern: Searching. About 15 minutes. Part of the Pro drill bank.
Daily sales sit in a list. A 'pivot' day is one where the sum on the left equals the sum on the right. Planning uses it as a mid-campaign checkpoint.
A finance analyst wants the day when the sales before it equal the sales after it, the balance point of a list of daily sales. Return the first index where the sum of the numbers to the left equals the sum of the numbers to the right. The number at the index itself is not counted on either side. If no index works, return -1. Write pivot_index(sales). In [1, 7, 3, 6, 5, 6] index 3 works: the left side sums to 1+7+3 = 11 and the right side to 5+6 = 11. In [1, 2, 3] no index works, so the answer is -1. The starter file already has the function name and its print lines. Keep them and fill in the body.
Requirements
- Compare the sum on the left with the sum on the right.
- The pivot value itself is excluded from both sides.
- Return the first (smallest) index that works.
- Return -1 if there is none.
Constraints
- sales is a list of numbers (it can contain negatives and zeros).
- An empty side counts as 0.
Examples
Input: pivot_index([1, 7, 3, 6, 5, 6]) Output: 3 This input follows the stated rules and produces this output.
Topics: lakebench, python, prefix-sums.
More Python interview questions · All interview problems · Learn data engineering
Find the pivot index of a sales list
Interview-style drill: Find the index where the sum of sales to its left equals the sum of sales to its right.
Find the pivot index of a sales list (left sum equals right sum).