Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the candidate for majority element.
DSA Go
candidate := nums[[1]]Attempts:
3 left
💡 Hint
Common Mistakes
Using index 1 instead of 0 causes skipping the first element.
Using len(nums) causes index out of range error.
✗ Incorrect
The candidate is initialized to the first element, which is at index 0.
2fill in blank
mediumComplete the code to reset the count when a new candidate is chosen.
DSA Go
if count == 0 { candidate = nums[[1]] count = 1 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 instead of i resets candidate always to first element.
Using count or candidate as index causes errors.
✗ Incorrect
When count is zero, the candidate is updated to the current element at index i.
3fill in blank
hardFix the error in the condition to update the count when the current element matches the candidate.
DSA Go
if nums[i] [1] candidate { count++ } else { count-- }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' causes count to increase on different elements.
Using '<' or '>' is incorrect for equality check.
✗ Incorrect
The count increases only when the current element equals the candidate.
4fill in blank
hardFill both blanks to complete the loop that finds the majority element candidate.
DSA Go
for [1] := 1; [1] < [2]; [1]++ { if count == 0 { candidate = nums[[1]] count = 1 } else if nums[[1]] == candidate { count++ } else { count-- } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using count or candidate as loop limit causes errors.
Starting loop at 0 repeats first element already used.
✗ Incorrect
The loop variable i starts at 1 and runs while i is less than the length of nums.
5fill in blank
hardFill all three blanks to verify the candidate is the majority element by counting its occurrences.
DSA Go
count = 0 for _, num := range nums { if num [1] candidate { count[2] } } if count [3] len(nums)/2 { return candidate } else { return -1 }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' instead of '>' causes wrong majority check.
Using '=' instead of '==' causes assignment error.
Forgetting to increment count causes zero count.
✗ Incorrect
We count how many times candidate appears (==), increment count (++), and check if count is greater than half the length (>).
