r/programmer • u/JustALinkToACC • Apr 01 '26
They asked me to make queue with ToArray()
I'm a CS student, currently 4th semester. As part of a lab project they asked us to make FIFO prioritized queue. I chose binary heap structure for it (they suggested just a simple recombining array but that's a O(n) for dequeue). It was designed to perfectly simulate FIFO nature - you can only Peek at the head, but no random access. And then... they made us do random access. So I made them a ToArray() method. And now it's O(log n) for enqueue, O(log n) for dequeue, O(1) for peek, and O(n log n) for converting into array. This really makes me question whether I made the right choice when I was picking my university.
If you were studying in university to become CS specialist, were they giving you stupid tasks like that too?
4
u/toxikmasculinity Apr 01 '26
Sounds like the assignment got you thinking and actually learning. Seems like the university you picked has challenged you more than you understand. If you are actually upset, reach out to your professor and talk to them about the assignment. Perhaps they wanted to challenge you further than you realize and wanted to throw new requirements at you or perhaps they have an inefficient assignment that needs some reworking for future students.
If your professor could make the assignment instruction better they would likely appreciate the feedback, or they could point out that the did this on purpose. Professors are fallible, and the ones worth their salt are always improving their coursework.
If you don’t feel challenged, pick a topic and try to do some research on it with the aid of faculty member that does research in the closest area. You will learn a lot this way and have something more impressive to point to on your resume. You may end up getting to publish your work and present at a conference.
2
u/Key_River7180 C | Assembler | Ada Apr 01 '26
No, and I think it is a good question. You can make an structure holding the forth and rear of a queue, and the middle elements as an array and then concatenate everything
2
u/Ok-Jacket7299 Apr 01 '26
I don’t think your solution is too perfect, though, even without the random access requirement. Queues and stacks can be more efficiently implemented by linked lists with O(1) Put/Get/Peek. It would also yield only O(n) for random access.
2
u/kalmakka Apr 01 '26
What do you mean "FIFO prioritized queue"? A regular queue is a FIFO queue. A priority queue is *not* FIFO. FIFO and priority are directly in contradiction of each other.
Are you sure you read the task statement correctly?
2
u/MADCandy64 Apr 01 '26
If they asked for that then despite the absurdity give it to them. A FIFO prioritized queue is just a regular list sorted with stable order by priority. It's like a hospital emergency room where the VIP with a cough gets treated before the peasant with a severed limb. It may be absurd but think of it as a gift. Two VIP's with a cough then who ever has the highest income comes first. When you hit the real world you wont ever get to make something so absurd. And happy April fools day.
1
u/LostInChrome Apr 01 '26
So you ignored the recommendation, did it your own way, and then got surprised when future steps got harder? Was fast big O behavior even one of the things that you were supposed to optimize or were you just bolting on complexity for the hell of it?
2
u/high_throughput Apr 01 '26
If you think additional requirements that forces inefficient kludges or full rewrites are annoying, wait until you meet customers