So one of my co-workers asked me this question and neither he nor I can figure it out... but apparently he was asked this question in an interview a while back... so I figured I'd ask the very smart people at MacRumors... The language doesn't matter too much but this is one of those "general" questions so it probably shouldn't use some super proprietary stuff or things like "Array.removeDuplicatesAndDontUseMoreMemory(a)".
The kicker is that last part... it's a very easy question if you can use an int or two to store the current index... but without that... is there something I'm missing or is the question just junk? Keep in mind the array could come in at any length with any items... it is a sorted, positive integer array though. But it might look like [15, 20, 30, 40, 40, 40, 542214].
My figuring is that once you've checked for a.length = 1 you could access the 1st and last indexes... the 2nd index might be another safe index but otherwise you're stuck with those I think. I also thought about using a known index to store the current index but if the array is already stripped of duplicates that doesn't work either.
Also keep in mind the question asks that the duplicates be removed and the size be returned.
Brain... broken. Now I'll be sad when the next 20 people answer it in seconds.
You get an honorary bamboo sheriffs badge for figuring it out.*
* Offer not valid.
Suppose you have an integer array a of size n that is ordered but has duplicates. For example a = [0, 0, 0, 2, 3, 3, 5, 5, 5, 7, 9, 9] (n = 12). Write a function that removes the duplicates, and then returns the new size. In above example, a becomes [0, 2, 3, 5, 7, 9] and the function returns 6. Do it in-space, meaning no extra memory space should be allocated to store intermediate or final results.
The kicker is that last part... it's a very easy question if you can use an int or two to store the current index... but without that... is there something I'm missing or is the question just junk? Keep in mind the array could come in at any length with any items... it is a sorted, positive integer array though. But it might look like [15, 20, 30, 40, 40, 40, 542214].
My figuring is that once you've checked for a.length = 1 you could access the 1st and last indexes... the 2nd index might be another safe index but otherwise you're stuck with those I think. I also thought about using a known index to store the current index but if the array is already stripped of duplicates that doesn't work either.
Also keep in mind the question asks that the duplicates be removed and the size be returned.
Brain... broken. Now I'll be sad when the next 20 people answer it in seconds.
You get an honorary bamboo sheriffs badge for figuring it out.*
* Offer not valid.