Yelp Interview Question

Why Yelp? Tell me about a project you worked on lately. Given an array, return the smallest k elements from the array.

Interview Answers

Anonymous

Jan 27, 2016

in python: sorted(array)[0:k]

1

Anonymous

May 23, 2016

Sorting is O(nlogn). Better time complexity, store in priority queue then return an array of the last k elements. Time complexity: O(n).

1

Anonymous

Sep 18, 2016

^ That is not O(n). It is O(n + klogn) and you can do better than that.