Showing posts with label Find first missing nonnegative number. Show all posts
Showing posts with label Find first missing nonnegative number. Show all posts

Monday, March 26, 2018

Find first missing nonnegative number

March 26, 2018

Introduction


It is the algorithm to find first missing nonnegative number in the array. What I like the algorithm is to work with the constraint. The constraint is that the array cannot be modified, and then second algorithm is to modify the array.

Code review


I like to work on the algorithm one more time. Here is my C# practice for the first solution without changing the input array. And here is the second solution to change the input array.

Actionable Items


Google and find algorithms in Leetcode related to the algorithm.


Sunday, February 25, 2018

Find first missing number

Feb. 25, 2018

Introduction


It is part of my training idea, practice until you cannot get it wrong. I had a mock interview this morning at 10:00 AM. I did write down the idea to do in place swap to find the first missing number. And then I wrote code with a few bugs, I failed a few test cases, and then I fixed the bugs on line 15 and also line 16.

Practice until you cannot get it wrong


One thing I like to train myself through mock interview is to pay attention to small detail. The peer asked me to work on the optimal solution using in place, and then I came out the idea but I did say something to change the value to negative one if the value is bigger than the array size. And the code I wrote was not correct, I ran into index-out-of-range error and failed a few test cases. I fixed the bugs of course very quickly.

Line 18 swap function should be called only after the index is in the array's range.

Here is C# code.

The mistakes I made in my first writing just reminds me that I have to train myself hard than before.

Sunday, January 28, 2018

Find first missing number

January 28, 2018


Introduction


It is the algorithm for me to work on this 12:00 PM mock interview. I like to review the analysis and code.


Code review 


Here is the code with the analysis.


Friday, November 3, 2017

Leetcode 41: First missing positive

Nov. 1, 2017

Plan to look into Leetcode and find the similar algorithm called get smallest nonnegative number.

Get smallest nonnegative number, do not change the array.

My C# practice code is here on Nov. 1, 2017

Also, I spent almost 50 minutes to interview the peer on Nov. 2, 2017 and had some discussion about the algorithm. I like the peer to go over the base cases to show very good reasoning.

[],   0
[0], 1
[1], 0
[0, 2, 3]  1
[3, 0 , 2] 1
[0, 1, 2, 10000]


Leetcode 41: First missing positive 


It is the hard level algorithm. Time complexity is O(n), space is O(1).