Pergunta de entrevista da empresa Yelp

Which would be a better data structure for searching: a linked-list or an arraylist?

Respostas da entrevista

Sigiloso

2 de mar. de 2011

An arraylist. A linked list always takes O(n) worst case for searching. On the other hand, an arraylist can be sorted one time, at cost of O(n log(n)). After that, we can do binary search which takes time O(log(n)). The average cost of a search after n searches for the arraylist would be (n log(n) + n * log(n) )/n = O(log(n)). Compare this is the average search time of a linked list: n * n/n = O(n).

4

Sigiloso

7 de ago. de 2010

I think the 2nd structure they gave was an arraylist; something similar. The point was that the 2nd structure allowed direct accesses, whereas the linked list would require traversing.