Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Vote For New Content
The first for-loop where you first iterate through the elements in the array to ...

Duc

Jul 1, 2022

The first for-loop where you first iterate through the elements in the array to insert to missingNumbers and extraNumbers lacks a condition I think. First, a Set does not tolerate duplicate, you can totally add a nums[i] if nums[i] != i + 1, that's true and I agree. However, what if 'i + 1' is equivalent to an element in the Set? For example, consider nums = [5, 6, 7, 8, 9] and k = 9. At i = 4, if I follow the solution's logic, missingNumbers will contain [1, 2, 3, 4, 5]. That 5 is a killer. I managed to fix this by adding a condition to only add i + 1 if extraNumbers has not contained it, and it works so far. Definitely fix me if I'm wrong tho.

0

0

Comments
Comments

On this page