Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line.
You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i].
Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.
Return the time taken for the person initially at position k (0-indexed) to finish buying tickets.
Example 1:
Input: tickets = [2,3,2], k = 2
Output: 6
Explanation:
Example 2:
Input: tickets = [5,1,1,1], k = 0
Output: 8
Explanation:
Constraints:
n == tickets.length1 <= n <= 1001 <= tickets[i] <= 1000 <= k < nProblem summary: There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line. You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i]. Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line. Return the time taken for the person initially at position k (0-indexed) to finish buying tickets.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[2,3,2] 2
[5,1,1,1] 0
number-of-students-unable-to-eat-lunch)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2073: Time Needed to Buy Tickets
class Solution {
public int timeRequiredToBuy(int[] tickets, int k) {
int ans = 0;
for (int i = 0; i < tickets.length; ++i) {
ans += Math.min(tickets[i], i <= k ? tickets[k] : tickets[k] - 1);
}
return ans;
}
}
// Accepted solution for LeetCode #2073: Time Needed to Buy Tickets
func timeRequiredToBuy(tickets []int, k int) (ans int) {
for i, x := range tickets {
t := tickets[k]
if i > k {
t--
}
ans += min(x, t)
}
return
}
# Accepted solution for LeetCode #2073: Time Needed to Buy Tickets
class Solution:
def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
ans = 0
for i, x in enumerate(tickets):
ans += min(x, tickets[k] if i <= k else tickets[k] - 1)
return ans
// Accepted solution for LeetCode #2073: Time Needed to Buy Tickets
struct Solution;
impl Solution {
fn time_required_to_buy(tickets: Vec<i32>, k: i32) -> i32 {
let mut res = 0;
let n = tickets.len();
let mut arr = vec![0; n];
let mut i = 0;
let k = k as usize;
while arr[k] != tickets[k] {
if arr[i] < tickets[i] {
arr[i] += 1;
res += 1;
}
i += 1;
i %= n;
}
res
}
}
#[test]
fn test() {
let tickets = vec![2, 3, 2];
let k = 2;
let res = 6;
assert_eq!(Solution::time_required_to_buy(tickets, k), res);
let tickets = vec![5, 1, 1, 1];
let k = 0;
let res = 8;
assert_eq!(Solution::time_required_to_buy(tickets, k), res);
}
// Accepted solution for LeetCode #2073: Time Needed to Buy Tickets
function timeRequiredToBuy(tickets: number[], k: number): number {
let ans = 0;
const n = tickets.length;
for (let i = 0; i < n; ++i) {
ans += Math.min(tickets[i], i <= k ? tickets[k] : tickets[k] - 1);
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.