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.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two integers n and k.
Initially, you start with an array a of n integers where a[i] = 1 for all 0 <= i <= n - 1. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, a[0] remains the same, a[1] becomes a[0] + a[1], a[2] becomes a[0] + a[1] + a[2], and so on.
Return the value of a[n - 1] after k seconds.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: n = 4, k = 5
Output: 56
Explanation:
| Second | State After |
|---|---|
| 0 | [1,1,1,1] |
| 1 | [1,2,3,4] |
| 2 | [1,3,6,10] |
| 3 | [1,4,10,20] |
| 4 | [1,5,15,35] |
| 5 | [1,6,21,56] |
Example 2:
Input: n = 5, k = 3
Output: 35
Explanation:
| Second | State After |
|---|---|
| 0 | [1,1,1,1,1] |
| 1 | [1,2,3,4,5] |
| 2 | [1,3,6,10,15] |
| 3 | [1,4,10,20,35] |
Constraints:
1 <= n, k <= 1000Problem summary: You are given two integers n and k. Initially, you start with an array a of n integers where a[i] = 1 for all 0 <= i <= n - 1. After each second, you simultaneously update each element to be the sum of all its preceding elements plus the element itself. For example, after one second, a[0] remains the same, a[1] becomes a[0] + a[1], a[2] becomes a[0] + a[1] + a[2], and so on. Return the value of a[n - 1] after k seconds. Since the answer may be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
4 5
5 3
left-and-right-sum-differences)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3179: Find the N-th Value After K Seconds
class Solution {
public int valueAfterKSeconds(int n, int k) {
final int mod = (int) 1e9 + 7;
int[] a = new int[n];
Arrays.fill(a, 1);
while (k-- > 0) {
for (int i = 1; i < n; ++i) {
a[i] = (a[i] + a[i - 1]) % mod;
}
}
return a[n - 1];
}
}
// Accepted solution for LeetCode #3179: Find the N-th Value After K Seconds
func valueAfterKSeconds(n int, k int) int {
const mod int = 1e9 + 7
a := make([]int, n)
for i := range a {
a[i] = 1
}
for ; k > 0; k-- {
for i := 1; i < n; i++ {
a[i] = (a[i] + a[i-1]) % mod
}
}
return a[n-1]
}
# Accepted solution for LeetCode #3179: Find the N-th Value After K Seconds
class Solution:
def valueAfterKSeconds(self, n: int, k: int) -> int:
a = [1] * n
mod = 10**9 + 7
for _ in range(k):
for i in range(1, n):
a[i] = (a[i] + a[i - 1]) % mod
return a[n - 1]
// Accepted solution for LeetCode #3179: Find the N-th Value After K Seconds
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3179: Find the N-th Value After K Seconds
// class Solution {
// public int valueAfterKSeconds(int n, int k) {
// final int mod = (int) 1e9 + 7;
// int[] a = new int[n];
// Arrays.fill(a, 1);
// while (k-- > 0) {
// for (int i = 1; i < n; ++i) {
// a[i] = (a[i] + a[i - 1]) % mod;
// }
// }
// return a[n - 1];
// }
// }
// Accepted solution for LeetCode #3179: Find the N-th Value After K Seconds
function valueAfterKSeconds(n: number, k: number): number {
const a: number[] = Array(n).fill(1);
const mod: number = 10 ** 9 + 7;
while (k--) {
for (let i = 1; i < n; ++i) {
a[i] = (a[i] + a[i - 1]) % mod;
}
}
return a[n - 1];
}
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.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.