Skip to main content

Score Validator - Solution & Explanation

EasyArrayStringSimulation9 min read
Practice this problem

Problem Statement

You are given a string array events.

Initially, score = 0 and counter = 0. Each element in events is one of the following:

  • "0", "1", "2", "3", "4", "6": Add that value to the total score.
  • "W": Increase the counter by 1. No score is added.
  • "WD": Add 1 to the total score.
  • "NB": Add 1 to the total score.

Process the array from left to right. Stop processing when either:

  • All elements in events have been processed, or
  • The counter becomes 10.

Return an integer array [score, counter], where:

  • score is the final total score.
  • counter is the final counter value.

 

Example 1:

Input: events = ["1","4","W","6","WD"]

Output: [12,1]

Explanation:

Event Score Counter
"1" 1 0
"4" 5 0
"W" 5 1
"6" 11 1
"WD" 12 1

Final result: [12, 1].

Example 2:

Input: events = ["WD","NB","0","4","4"]

Output: [10,0]

Explanation:

Event Score Counter
"WD" 1 0
"NB" 2 0
"0" 2 0
"4" 6 0
"4" 10 0

Final result: [10, 0].

Example 3:

Input: events = ["W","W","W","W","W","W","W","W","W","W","W"]

Output: [0,10]

Explanation:

After 10 occurrences of "W", the counter reaches 10, so processing stops. The remaining events are ignored.

 

Constraints:

  • 1 <= events.length <= 1000
  • events[i] is one of "0", "1", "2", "3", "4", "6", "W", "WD", or "NB".

Approach Overview

Problem Overview: You are given a reported total score and a list of individual scoring events. The task is to verify whether the recorded total is valid based on the events provided. If the sum of all scoring events matches the reported score (and follows any basic constraints like non‑negative scoring), the score is considered valid.

Approach 1: Recompute Total (Brute Force) (Time: O(n), Space: O(1))

The most direct approach recomputes the score from scratch. Iterate through the list of scoring events and accumulate their values into a running total. After processing all events, compare the computed sum with the reported score. If they match and each event value satisfies basic constraints (such as non-negative or within allowed limits), the score is valid. This approach uses simple iteration and arithmetic, making it easy to implement and reason about.

Approach 2: Single-Pass Validation (Optimal) (Time: O(n), Space: O(1))

A slightly more defensive version performs validation during the same traversal. Iterate through the events while maintaining a running sum. At each step, check whether the event value is within the allowed scoring range and update the cumulative score. If any invalid value appears, terminate early. After the traversal finishes, verify that the cumulative sum equals the reported total. This still runs in linear time but catches invalid inputs earlier, which is useful when working with large datasets.

Approach 3: Prefix Tracking for Streaming Data (Time: O(n), Space: O(1))

If the scoring events arrive as a stream rather than a fixed list, maintain a running prefix sum and validate each update as it arrives. Each new event is added to the current total, and the system checks constraints immediately. This pattern is common in real-time score tracking systems and uses the same constant memory footprint while processing events sequentially.

Recommended for interviews: The single-pass validation approach is what interviewers typically expect. It demonstrates clear reasoning: iterate once, maintain a running total, and verify constraints while computing the final score. The brute force recomputation also works but mainly shows the baseline idea. Understanding linear scans and cumulative calculations—core techniques in arrays and iteration problems—helps you quickly recognize and solve similar validation tasks.

Solution

We can directly simulate the process described in the problem to calculate the final score and counter value.

First, we initialize two variables score and counter, representing the current total score and counter value respectively. Then we iterate through each event in the array events and update score and counter based on the event type:

  • If the event is a numeric string, we convert it to an integer and add it to score.
  • If the event is the string "W", we increment counter by 1 and check if it has reached 10; if so, we stop processing.
  • Otherwise (the event is "WD" or "NB"), we add 1 to score.

After processing all events or when the counter reaches 10, we return an array containing the final values of score and counter.

The time complexity is O(n), where n is the length of the array events. The space complexity is O(1), as we only use a constant amount of extra space.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Recompute Total (Brute Force)O(n)O(1)Simple validation when you only need to check the final total
Single-Pass ValidationO(n)O(1)General case where you want to detect invalid events early
Streaming Prefix TrackingO(n)O(1)When events arrive incrementally in a stream or live system

Video Solution

Score Validator | Leetcode Biweekly Contest 182 | Q1 | Leetcode 3921 | Array | Simulation | EasyPragya Gupta134 views views

Watch 5 more video solutions →

Frequently Asked Questions

Is Score Validator easy or hard?
Score Validator is generally considered an easy problem. The core idea is verifying a computed total using a linear scan through the input array. It focuses on basic iteration, arithmetic checks, and careful validation of constraints.
Score Validator Python/Java solution
In both Python and Java, the solution typically loops through the array of events, adds each value to a running total, and checks constraints along the way. After the loop, compare the calculated sum with the reported score. The logic is identical across languages and runs in O(n) time with O(1) space.
How to solve Score Validator in O(n)?
Traverse the scoring events once while maintaining a cumulative sum. During the iteration, validate that each event value falls within allowed constraints and update the running total. After the traversal, compare the calculated sum with the reported score to determine validity.
What is the best approach for Score Validator?
The best approach is a single-pass validation. Iterate through the list of scoring events, maintain a running sum, and verify that each event value is valid. After processing all events, check if the computed sum matches the reported score. This method runs in O(n) time and uses O(1) space.
Is Score Validator asked at Google/Amazon/Meta?
Problems that involve validating totals, cumulative sums, and input constraints appear frequently in interviews at companies like Amazon and Google. While the exact problem name may vary, the underlying pattern—single-pass validation over an array—is common in coding interviews.
What data structure is used in Score Validator?
The primary data structure is an array or list storing the scoring events. The algorithm processes the array sequentially and maintains a running total using simple variables, which keeps space usage constant.
What is the time complexity of Score Validator?
The typical solution runs in O(n) time, where n is the number of scoring events. Each event is processed exactly once while updating a running total. The algorithm uses O(1) additional space because it only stores a few counters.

Ready to solve this problem?

Practice Score Validator with our built-in code editor and test cases.

Practice on FleetCode