DumbCoder Sort an set of Number occuring in duplicates in array
Hello geniuses!
So, just today I finished up with an interview and realized how bad I am as a Problem Solver once again.But nothing is gonna stop me and I am soon gonna become RED! Because dumb people too can make it big……Hhahahaha.
Initial Approach
I will only focus on the Coding problem for now.
So the problem given to me was a Standard Problem
**Sort the array consisting of 0s, 1s & 2s**
So I heard and solved it on leetcode before and had a idea instantly to Use 2/3 pointers approach.Now the interviewer asked me to code the solution. So I began coding without any algorithm and thought process and after some time came with a code and a bunch of changes which is exactly as it looks like I just copied from collabedit.
So after writing while dry run too i made a few changes too. So while somebody watched my code why i couldn’t think and write in a few mins time knowing the question before hand too.
- The reasons
- Dry run in my mind before of approach before you jump in code.
- Write the algorithm in steps before hand.
- So the interviewer went on to ask a follow up on
- What if there is 0s,1s,2s,3s what can be done
- And if there are 0s,1s,….ns dicuss upon a scalable approach he asked
So that point i thought for around a 30 sec and was quite a blank and told i don’t know .
My mistakes 1. Take more time and use pen and paper. 2. Make Use some todo checklist
Then he told me about Counting Sort and at that moment I told i don’t have idea about that and asked is it similar to Bucket/Radix Sort and moved on.
So immediately after the interview without any google search I thought of it Counting Sort I think I got the correct answer then just after 2 mins don’t know why I couldn’t think of that time
So i guess it’s like u
Time Comp - O(n) Space Comp - O(n)
Updated Solution-Code and analysis of Mistakes
My code complexity are
Time & Space Complexity - O(n)
- Some pecularities/differences in there
- As keys would always be less than total elements.
- Haven’t used Array to store range of keys [Min-Max]
- So due to this the changed time & space complexity becomes O( n + range of key).
- We could have used Normal hashmap and then sorted using comparator as well.
So, Yes that’s it for now. If any suggestions please comment.