aklepatc ([personal profile] aklepatc) wrote2024-05-08 01:34 pm

Strange Container

Code a data structure with 2 operations: push() and popMostFrequent().

Most of the meat is in the comments/discussion below.

[personal profile] sassa_nf 2024-05-08 05:51 pm (UTC)(link)
Most frequently accessed?..

Ah, OK, there are no other accesses, just pushes and pops that presumably remove items.
Edited 2024-05-08 17:53 (UTC)

[personal profile] sassa_nf 2024-05-08 09:23 pm (UTC)(link)
It was a bit more to type on the phone. :)

Performance-wise - ... it's a slice, they do not necessarily move the data. I don't know what they do, but I wouldn't assume.

[personal profile] sassa_nf 2024-05-09 07:35 am (UTC)(link)
Yes, almost. The difference really isn't the memory retention, but when reallocation happens and what the footprint requirement is.

I.e if you pop from the back, you retain just as much memory as when you pop from the front. The difference is that if you pop from the front, this will trim to size upon the next push, but if you pop from the back, you will retain the historically biggest array.

But we can't really tell how that works, because it is a slice, an abstraction. So it may well be a ring buffer underneath, why not?

Of course, the experiment shows that the implementation isn't a ring buffer:

https://tio.run/##bY7NCsIwEITv@xTLnhIoUvFW6Dt4Lz3E/qXUbENNQSM@e4ypCIqngfm@gRnmEKxqJjV0aNTIAKOx8@IEUm8coQToV24SExLvgIgGizIWUyeq@uKWkYcM8wwPcoMlKms7boXJkNpZUwx1aihhTttqX9R/5OvNv60qryMi7z1tq4/H0dNaf3tudQSvIn7eHeMjd2Zh5G/DEh4hPAE

Append behaviour was surprising, though. I.e. that appending to n modified m.

[personal profile] sassa_nf 2024-05-09 04:34 pm (UTC)(link)
Well, it is surprising to those familiar with java byte buffers. Slicing a buffer there creates a slice of the underlying buffer, but does not share its limit - can't go beyond the last byte available in the buffer at the time of slicing, even if the capacity allows for that.

So to speak, cap(n)==len(n) immediately after slicing.

[personal profile] sassa_nf 2024-05-09 07:42 am (UTC)(link)
Good point about clearing the reference. But the order may matter.

Eg

v, c.L[j], c.L[j][0] = c.L[j][0], c.L[j][1:], nil

c.L[j][0] on the left - does that refer to the c.L[j] before or after assignment of c.L[j][1:] to it?
juan_gandhi: (Default)

[personal profile] juan_gandhi 2024-05-08 07:49 pm (UTC)(link)

PriorityQueue, I guess.

juan_gandhi: (Default)

[personal profile] juan_gandhi 2024-05-08 08:21 pm (UTC)(link)

Any implementation of priority queue, I mean.

juan_gandhi: (Default)

[personal profile] juan_gandhi 2024-05-08 09:08 pm (UTC)(link)

So, it's about interviewers. You worked in companies. You know what kind of people are there. Random. So... so that's what you get. Besides, if you show your expertise, they'll be jealous.

juan_gandhi: (Default)

[personal profile] juan_gandhi 2024-05-08 09:08 pm (UTC)(link)

I see. It was a philosophical question. But I was also curious... but not much. :)