Question d’entretien chez Yelp

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

Réponses aux questions d'entretien

Utilisateur anonyme

2 mars 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

Utilisateur anonyme

7 août 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.