Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
Your music player contains n different songs. You want to listen to goal songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that:
k other songs have been played.Given n, goal, and k, return the number of possible playlists that you can create. Since the answer can be very large, return it modulo 109 + 7.
Example 1:
Input: n = 3, goal = 3, k = 1 Output: 6 Explanation: There are 6 possible playlists: [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1].
Example 2:
Input: n = 2, goal = 3, k = 0 Output: 6 Explanation: There are 6 possible playlists: [1, 1, 2], [1, 2, 1], [2, 1, 1], [2, 2, 1], [2, 1, 2], and [1, 2, 2].
Example 3:
Input: n = 2, goal = 3, k = 1 Output: 2 Explanation: There are 2 possible playlists: [1, 2, 1] and [2, 1, 2].
Constraints:
0 <= k < n <= goal <= 100Problem summary: Your music player contains n different songs. You want to listen to goal songs (not necessarily different) during your trip. To avoid boredom, you will create a playlist so that: Every song is played at least once. A song can only be played again only if k other songs have been played. Given n, goal, and k, return the number of possible playlists that you can create. Since the answer can be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math · Dynamic Programming
3 3 1
2 3 0
2 3 1
count-the-number-of-good-subsequences)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #920: Number of Music Playlists
class Solution {
public int numMusicPlaylists(int n, int goal, int k) {
final int mod = (int) 1e9 + 7;
long[][] f = new long[goal + 1][n + 1];
f[0][0] = 1;
for (int i = 1; i <= goal; ++i) {
for (int j = 1; j <= n; ++j) {
f[i][j] = f[i - 1][j - 1] * (n - j + 1);
if (j > k) {
f[i][j] += f[i - 1][j] * (j - k);
}
f[i][j] %= mod;
}
}
return (int) f[goal][n];
}
}
// Accepted solution for LeetCode #920: Number of Music Playlists
func numMusicPlaylists(n int, goal int, k int) int {
const mod = 1e9 + 7
f := make([][]int, goal+1)
for i := range f {
f[i] = make([]int, n+1)
}
f[0][0] = 1
for i := 1; i <= goal; i++ {
for j := 1; j <= n; j++ {
f[i][j] = f[i-1][j-1] * (n - j + 1)
if j > k {
f[i][j] += f[i-1][j] * (j - k)
}
f[i][j] %= mod
}
}
return f[goal][n]
}
# Accepted solution for LeetCode #920: Number of Music Playlists
class Solution:
def numMusicPlaylists(self, n: int, goal: int, k: int) -> int:
mod = 10**9 + 7
f = [[0] * (n + 1) for _ in range(goal + 1)]
f[0][0] = 1
for i in range(1, goal + 1):
for j in range(1, n + 1):
f[i][j] = f[i - 1][j - 1] * (n - j + 1)
if j > k:
f[i][j] += f[i - 1][j] * (j - k)
f[i][j] %= mod
return f[goal][n]
// Accepted solution for LeetCode #920: Number of Music Playlists
impl Solution {
#[allow(dead_code)]
pub fn num_music_playlists(n: i32, goal: i32, k: i32) -> i32 {
let mut dp: Vec<Vec<i64>> = vec![vec![0; n as usize + 1]; goal as usize + 1];
// Initialize the dp vector
dp[0][0] = 1;
// Begin the dp process
for i in 1..=goal as usize {
for j in 1..=n as usize {
// Choose the song that has not been chosen before
// We have n - (j - 1) songs to choose
dp[i][j] += dp[i - 1][j - 1] * ((n - ((j as i32) - 1)) as i64);
// Choose the song that has been chosen before
// We have j - k songs to choose if j > k
if (j as i32) > k {
dp[i][j] += dp[i - 1][j] * (((j as i32) - k) as i64);
}
// Update dp[i][j]
dp[i][j] %= ((1e9 as i32) + 7) as i64;
}
}
dp[goal as usize][n as usize] as i32
}
}
// Accepted solution for LeetCode #920: Number of Music Playlists
function numMusicPlaylists(n: number, goal: number, k: number): number {
const mod = 1e9 + 7;
const f = new Array(goal + 1).fill(0).map(() => new Array(n + 1).fill(0));
f[0][0] = 1;
for (let i = 1; i <= goal; ++i) {
for (let j = 1; j <= n; ++j) {
f[i][j] = f[i - 1][j - 1] * (n - j + 1);
if (j > k) {
f[i][j] += f[i - 1][j] * (j - k);
}
f[i][j] %= mod;
}
}
return f[goal][n];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.