Magicsheet logo

Sum of Scores of Built Strings

Hard
25%
Updated 8/1/2025

Sum of Scores of Built Strings

What is this problem about?

The "Sum of Scores of Built Strings interview question" is a high-level string manipulation challenge often encountered in advanced technical interviews. The problem involves a string that is constructed step-by-step. For each suffix of the string (starting from the last character and growing backwards to the full string), you need to find the "score." The score is defined as the length of the longest common prefix (LCP) between that suffix and the original full string. The final answer is the sum of these scores.

Essentially, you are looking at every prefix of the string and asking: "How much of this prefix matches the start of the entire original string?" This requires an efficient way to compare multiple substrings without redundant calculations.

Why is this asked in interviews?

This "Sum of Scores of Built Strings coding problem" is a favorite of top-tier companies like Amazon because it tests a candidate's knowledge of advanced string algorithms. It separates candidates who know basic loops from those who understand:

  1. String Hashing/Rolling Hash: Efficiently comparing substrings in constant time.
  2. Z-Algorithm: A specialized linear-time algorithm for finding LCPs of all suffixes with the prefix.
  3. Binary Search on Answers: Combining hashing with binary search to find the maximum matching length.
  4. Complexity Analysis: Ensuring the solution runs in O(N) or O(N log N) rather than O(N^2).

Algorithmic pattern used

The "String Matching interview pattern" here is best addressed using either the Z-Algorithm or Rolling Hash. The Z-Algorithm generates a "Z-array" where Z[i] is the length of the longest common prefix between the string and its suffix starting at index i. Summing this array directly provides the answer. Alternatively, using a "Rolling Hash interview pattern" allows you to perform binary search on the length of the prefix to find the largest match for each suffix.

Example explanation

Let’s take the string "azbaz". The suffixes (starting from the full string down to the last char) are:

  1. "azbaz": LCP with "azbaz" is 5.
  2. "zbaz": LCP with "azbaz" is 0.
  3. "baz": LCP with "azbaz" is 0.
  4. "az": LCP with "azbaz" is 2.
  5. "z": LCP with "azbaz" is 0.

Total Score: 5 + 0 + 0 + 2 + 0 = 7. Notice how we compare the start of each suffix with the start of the original string. The challenge is doing this for strings that are 100,000 characters long.

Common mistakes candidates make

  • Time Limit Exceeded (TLE): The most common mistake is using a simple nested loop to compare characters. For a string of length N, this results in O(N^2), which is too slow for large inputs.
  • Hash Collisions: If using rolling hashes, candidates may use a single hash or a small prime, leading to collisions where different strings appear to be the same.
  • Off-by-one errors: Managing indices in Z-algorithm or during binary search can be tricky, often leading to scores that are slightly off.

Interview preparation tip

For "Hard String interview patterns", familiarity with the Z-Algorithm is a significant advantage. It is a linear-time algorithm that specifically solves the "longest common prefix with the whole string" problem. If you find the Z-algorithm difficult to remember, master "Rolling Hash" as a versatile fallback. Always use two different base/modulus combinations for your hash to minimize the chance of collisions in an interview setting.

Similar Questions