Amazon Interview Question
SDE1sTeam: Amazon Fresh
Country: United States
Interview Type: Phone Interview
Hello ? list supports fast (O(1) ) insertion and deletion at any position, array only support fast insertion and deletion at the end (all other positions would take O(N) time).
Actually, arrays cannot insert or delete at the end. To increase or the decrease the size, you need to copy the array..
Arrays actually do not have fast insertion or deletion at the end.
You would need to create a new array with +1 or -1 size and copy all the elements to insert or remove in the end.
Linked list also does not have O(1) insertion because linked lists need to traverse down a certain amount and then create a new link, which has the worst complexity of O(n)
Arrays advantages:
- Javeed March 12, 2015- O(1) lookup time on any element regardless of relative position within the array
- No element in the array has any other element as a dependency, all can be edited or manipulated independently
- (In some languages) fixed size in memory, usually contiguous
- if ordered for whatever type of values are contained, lookup time goes from O(n) down to O(log n), whereas linked lists are always O(n) search
Linked Lists:
- Flexible data structure size, can grow or shrink as needed, better use of memory space
- elements in list are aware of/affected by other elements because of the linking, making this ideal for situations where you need to keep track of order/relation of elements (think stacks, queues)
- deletion of elements does not create blank gaps in the list, you can traverse the entire length and only need to be concerned about encountering a null pointer at the end