-
Notifications
You must be signed in to change notification settings - Fork 238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AdvancedLRUCache solution has bugs and wrong complexity rating #142
Comments
Note, it would be more standard to pass in a |
Also, could someone be confused by:
|
other bug in given solution:
this does not cause a re-sort of the priority queue so it isn't clear when this item will be evicted from the queue. |
I think this is just a bad test, as the number of edge cases and performance problems/opportunities could cause a stall-out in advanced programmers who know that there is no simple performant solution to this that isn't too complex to solve during a coding challenge. |
Using a Priority Queue on a data class with default |
The solution claims it is
O(1)
yet it isO(log(N))
due to the PriorityQueue offer/poll complexity. And really would need to beO(N)
because to update thelastUsed
time it would need to useremove(item); offer(item)
and that isO(N)
complexity onremove(item)
The advanced LRU has a bug:
You cannot mutate the value used for calculating priority for the PriorityQueue and expect that the order will change. The item must be removed and added back to the PriorityQueue after this mutation.
Other bug in the description vs. solution, considering
CacheItem
methodcompareTo
The question description says:
So that would mean you would need a second PriorityQueue ordered by
priority rating + last used
, otherwise you are removing within a combined sort order of expiry time + priority instead of just by priority. The above sort helps to be smart within an expiry time window for times, but those are less likely as expiry times will vary by nanoseconds/milliseconds due to timing of inserts.So the current implementation orders by more than the spec requires (for expiry time with secondary sort orders), and does not handle priority ordering correctly.
The text was updated successfully, but these errors were encountered: