Walmart Coding Question – Solved

10 Live
Counting Repeated Game Scores During a school sports day event, students record their scores, However, some students accidentally recorded the same score multiple times. Your task is to help the event organizer identify all scores that are repeated and determine the indices where each duplicate occurs. You are given an integer N, representing the number of students, followed by a list of their scores. For each score that is repeated, output the score, followed by the letter 'T, and then the indices where the score appears. If no duplicates are found, return -1. Input Format The first line contains an integer N - the number of students. The next N lines each contain an integer, representing a score recorded by a student. | Output Format For each score that appears more than once, print the score, followed by the letter'l', and then the indices where the score appears, separated by spaces. If no scores are repeated, output -1.

Asked in: Walmart

Image of the Question

Question Image

All Testcases Passed βœ”



Passcode Image

Solution


n = int(input())
h = {}
flag = True
for i in range(n):
// ... rest of solution available after purchase

πŸ”’ Please login to view the solution

Explanation


```
To solve the problem of counting repeated game scores and identifying their indices, begin by carefully analyzing the input and output requirements, and then think through a systematic method to track the occurrences of each score efficiently.

Step 1: Understand the Input and Output
- Input consists of an integer N denoting the number of students.
- Then N integers follow, each representing a student's score.
- The task is to find all scores that appear more than once (duplicates).
- For each duplicated score, print the score itself, followed by the letter 'l' (or 'I' as indicated), and then the list of indices (positions) where the score occurs.
- Indices are typically zero-based or one-based; clarify from the problem statement which indexing to use. Usually, in such cases, zero-based indexing is common unless otherwise specified.
- If no duplicates exist, print -1.

Step 2: Data Structures to Use
- To efficiently find duplicates and record their indices, consider using a dictionary or hashmap structure.
- The key will be the score.
- The value will be a list of indices where the score appears.
- This allows efficient insertion and retrieval as you iterate through the scores.

Step 3: Iteration Over Scores
- Iterate through the list of scores from index 0 to N-1.
- For each score, append the current index to its corresponding list in the dictionary.
- If the score is not yet in the dictionary, create a new entry with a list containing the current index.

Step 4: Identify Duplicates
- After processing all scores, iterate through the dictionary entries.
- For each score, check the length of the list of indices.
- If the length is greater than 1, it indicates the score appeared multiple times.
- Collect these scores and their index lists for output.

Step 5: Formatting Output
- For each duplicate score, output the score followed by the letter 'l' and then the indices separated by spaces.
- Ensure to output the indices in the order they were recorded, which should naturally be the case if you appended them during the iteration.
- If no duplicates exist, output -1.

Step 6: Handling Edge Cases
- All scores could be unique, so output must be -1.
- All scores could be identical, so all indices should be listed.
- Scores can have varying values, positive or negative, but since problem statement does not specify otherwise, assume integer values including possibly zero.
- Confirm whether indices should be zero-based or one-based from problem statement; adjust output accordingly.

Step 7: Complexity and Efficiency
- This approach requires only a single pass through the scores array to build the dictionary.
- Then one pass through the dictionary keys to find duplicates.
- Overall time complexity is O(N), which is efficient for large inputs.
- Space complexity is O(N) for the dictionary to store indices.

Step 8: Summary
- Use a dictionary mapping each score to a list of its indices.
- Populate this dictionary in one pass.
- Identify all keys with multiple indices.
- Format output by printing score, letter 'l', and indices.
- If no duplicates found, print -1.

This method ensures the problem is solved cleanly and efficiently without unnecessary repeated traversals or complex data structures.
```


Related Questions