The Restore The Array interview question gives you a string of digits and an integer k. You must count the number of ways to split the string into a sequence of positive integers (no leading zeros, each value between 1 and k inclusive) such that concatenating them all gives back the original string. The result should be returned modulo 10^9 + 7.
Google and Postman ask this HARD dynamic programming problem because it tests the ability to apply DP to string segmentation with numerical constraints. It combines digit-string parsing with DP state transitions and requires careful handling of leading zeros and value-range bounds. String DP with numeric constraints appears in parsing, compiler design, and data validation systems.
The pattern is dynamic programming on string indices. Define dp[i] = number of ways to split s[i:] into valid integers. Iterate from the end to the start. For each index i, try all valid segment lengths starting from s[i]: take 1 to len(str(k)) digits, parse the value, and if it is between 1 and k (and has no leading zero), add dp[i + segment_length] to dp[i]. Base case: dp[n] = 1 (empty suffix has one way to be split: the empty split).
s = "1317", k = 2000.
dp[4] = 1 (base).
Result: 8 ways.
s[i] == '0', no valid segment starts at i.k, which bounds the search.For the Restore The Array coding problem, the string and dynamic programming interview pattern is the approach. Define your DP clearly: dp[i] = ways to split s[i:]. Iterate backwards. The key optimization: limit segment length to len(str(k)) to avoid scanning unnecessarily long substrings. Google interviewers value concise DP definitions and clean transition logic — write the recurrence on the board before coding.