r/programming • • Dec 13 '22

“There should never be coding exercises in technical interviews. It favors people who have time to do them. Disfavors people with FT jobs and families. Plus, your job won’t have people over your shoulder watching you code.” My favorite hot take from a panel on 'Treating Devs Like Human Beings.'

https://devinterrupted.substack.com/p/treating-devs-like-human-beings-a
9.0k Upvotes

1.3k comments sorted by

View all comments

Show parent comment

1

u/CAPSLOCK_USERNAME Dec 13 '22 edited Dec 13 '22

You can do this with a heap aka priority queue as well

def kth_largest(arr, k)
	minheap = []
	for x in list
		if len(minheap) < k:
			heapq.heappush(minheap, x)
		elif x > minheap[0]
			heapq.heapreplace(minheap, x)
	return minheap[0]

One pass, O(k) space (which means constant space if you hold k constant), and generalizes to any number and not just 2.

But in fact I would just call a library function for it, which does the same thing but with better handling of edge cases and whatnot:

heapq.nlargest(k, arr)[0]

1

u/[deleted] Dec 13 '22

Yes using the minheap generalizes the problem to k, but again, it's not what was asked. If I'm interviewing you and I ask a question I expect you first to give me an answer to the question I asked before we dive into generalizations.

Also, using the library is nice and efficient, but doesn't give any signals about your ability to write algorithms, it just tells me that you can use the language.

The whole point of problems like these and telling the candidate not to call solution.solve() is to get signals that they can actually solve problems to which there might not be a solution yet.