2CunningHam
BAN USER
Comments (3)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
Use a Queue with a size limit of n, whenever n is exceed dequeue. Do this through the whole file if reading forward is the only option available.
If there is a way to read the file stream in reverse, then that's the best option.
Comment hidden because of low score. Click to expand.
0
of 0 vote
I'm not sure if that is possible in O(n) time. I would suggest the following algorithm:
Loop through the linked list:
Add to min or max binary heap
While the heap is not empty:
Pop off the heap and print
The complexity is O(n log(n)) on average it would be O(n) time since inserting on a heap approximates constant time (according to wikipedia article on it).
Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
- 2CunningHam December 15, 2015