Amazon Interview Question
Software Engineer / DevelopersThe point of doubling in size is to reduce the amortized cost per operation to constant time. Any constant proportion will do, but common values are 2x or 3/2 times the current size (see the Wiki page for dynamic array).
For an array with millions of entries, I would say collect some statistics (of how fast and how often the array grows) and pick a constant proportion based on heuristics.
As Dynacmic array is required when we need to store more elements than the size of old array. hence it would be - Summ of : old array size + Extra Element count.
NewArrazySize = OldArray.Size() + ExtraElementCount();
PS: If we double the size of the arryay , memory will be wasted incase the extra elements are not equal to old array size.
Correct me if I am wrong.
Thanks
CG
thats right.. thats why he was interested in knowing how much should you increase the size!
Usually in any "smart" implementation of dynamic array, it's size increases 2 times.
- gevorgk March 19, 2010