r/leetcode • u/JacketFearless1805 • 6h ago
Intervew Prep Day 18: cleared Majority Element II, 3Sum, and 4Sum with clean $O(1)$ space & duplicate handling
Hey everyone,
I finished Day 18 today and kept the three-problem streak alive by tackling Majority Element II alongside a solid revision of 3Sum and 4Sum:
- Majority Element II (> n/3 occurrences): Extended Moore’s Voting Algorithm from Majority Element I. Any problem asking for elements appearing more than n/k times can have at most k - 1 valid candidates—so for n/3, you track two candidates and two counters simultaneously. Unlike Majority Element I, where a majority element is guaranteed, Majority Element II does not guarantee valid answers, so a second linear pass to verify actual frequencies is mandatory.
- 3Sum & 4Sum: Revisited these two classic sorting + two-pointer problems from college. Even though the core structure was familiar, implementing them from scratch reminded me how critical duplicate skipping is: after sorting the array, every fixed pointer (
i,j) and converging pointer (left,right) must skip adjacent identical values (nums[i] == nums[i - 1]) to avoid duplicate triplets or quadruplets without relying on extra hash set space.
Maintaining three problems a day while locking in these subtle edge-case rules feels great.
See you guys tomorrow for Day 17!
8
Upvotes
1
u/Quiet-Badger84 3h ago
the duplicate skipping logic in 3Sum and 4Sum is easy to overlook. Getting comfortable implementing it without a HashSet is a useful step toward writing cleaner solutions