
A frontend interview problem where you assign frequency values to buckets by dividing by bucket size and flooring the result. Values exceeding the maximum range get clamped to the final bucket, and each bucket's array is sorted for output.
Chapters
Another interview question that is asked in 2024, interview question for a frontend developer. It's a question about frequencies and buckets. So we're given an array of frequencies and we're given a number of buckets, which is 10, and we're given a size of bucket. And what we need to do in this question is to sort all these frequencies into the buckets. So, um, so all the numbers are— I'll assume they start with 0, and in this case I think it's up to 1000. Okay, this is our case. So we know the numbers start with 0, and the output, and the output should be for every bucket, the collection of frequencies related to this bucket. So for example, bucket 0 will contain an array of numbers, so 8 for example, 4, 9, etc. So the bucket size is 10, so all the numbers from 0 to 9. For bucket number 1, it will be similar, but all the numbers from 10— I don't know if I have 10, but some number from 10 to 19, etc. And the last bucket, which is bucket number 9, will— so we can see that, um, if we have 10 buckets and each bucket size is 10, so maximum we can have numbers up to 100, right? So what's— so for the numbers that are larger than 100, we want to store them in the last bucket. So for example, 750, it's larger than 100, so this number also gonna be stored in the last bucket. So up to the last bucket, uh, bucket behave, uh, like a regular bucket, but for the last one, we're storing all the numbers that related to this bucket or larger than 100. Okay, so, so the output in this case will be and number like 90, but also, um, it will contain all the large number like so. Okay, so we have our function sort frequencies, and when we implement it, we will have the result. We're gonna pass bucket size, and then we can console.log our frequencies. So let's start writing the function. So we're gonna have a result that, um, we will have— we will call it frequencies, frequencies, and it will be our map We'll have a map. And so now we go over, um, our array in a for loop. Okay, so I, as you can see, I have, uh, um, Copilot is autocompleting me, but that's not all. So for every item in the array we divide it by bucket size in order to know bucket index. So for example, if we have 68, we divide it by 10 and we, um, we convert it to integer. So 6.8 integer is flooring it, it will be 6. So this means that 68 is in bucket number 6. Okay, so we have a bucket number, and, and, um, so now we need to go and check, okay, do we have already collection for bucket number in our result? If we don't have, initiate a collection for bucket number 6 with the array, and then we can push our item into the array of result. But that's not all, because in this case we are storing more than buckets now, more than 100 buckets, because in this case, um, divided by, by 10 floor unit, we have a bucket number 76. Okay, and we, and we cannot have it because our buckets number, um, is 10, and the idea of 10 buckets number, it goes from 0 to 9. Um, so not, not in general just 10, any 10. No, it's just from 0 to 9, 10 buckets. Okay, so, so we need to handle this case when our number is larger than 100. Okay, so 10 multiplied by 10, it's 100. So max number is 100. So in this case, um, we need to ask the question. So if our bucket index larger than bucket num, then we don't continue, but we put bucket index to be the last bucket. Okay, and in this case I need to convert it to let. Okay, so we're asking if it's larger than the buckets_num, then we put it as the last bucket, and then everything behaves the same, and at the end we return frequencies. So let's run it and see how it goes. Um, so we have our map and we have the numbers here, and, uh, well, they're not sorted, so it's hard to see. But for better visual effect, so you can see, we will just, uh, gonna sort them. Uh, so not object here, but object values. So go for all the values, and I'm gonna I'm gonna just sort it. It returns— in this case, I will just sort every bucket and then we will have more better visual representation. Okay, so let's see. For bucket number 0, we have number from 0 to 9. Market 1 is from 10 to 19. Okay, so we can, we can see that clearly. And what happened with the last bucket? So we have number up to 100, but then all the rest of the numbers that are larger than 200 also get into these buckets. And what is interesting, because we have buckets now and bucket size, we can actually change the number of buckets. So we can try, okay, what happens if I have like 3 buckets and it's bucket number 10? So What does it mean? What do we expect? We expect that everything behaves the same, just we will have only 3 buckets, but the last, the third one will contain most of the numbers. Okay, so let's, let's, uh, clear it and run it again. And here, don't even display them. I don't know, doesn't even display them. Okay, but we have again same 0 bucket, same second bucket, but the rest— the second bucket is actually from 9 to 30, but now it has all the rest of the numbers. I don't know, it shows more. Yeah, okay, it shows more. So, but, but also we can try to see, okay, what happens if the bucket size is different. So let's assume our bucket size is, for example, 40. Just out of my head, just random number, 40. Okay, so what does it mean? It means from 0 to 40, to 39, we have all the numbers in our first bucket. Second bucket is 42 80. Okay, so it behaves the same. And the last bucket is 80, obviously, to 120, but then we have all the rest of the numbers. Okay, so, but if we increase, it will be 220. So let's go 4 buckets, 4 buckets. Yeah, so Every bucket size is 40. Okay, so here we have up to 40, to 80, 220. We don't have 220, but, um, and the last one is 220, from 220 to 260. Okay, 200, but it's 260, but it's also contain all the rest of the numbers. So, okay, let's increase bucket number again by 5, and then we'll see this example. Yeah, so the third one, we see only 3 numbers, uh, up to 200, 160, from 120 to 160, and then from 160 to containing all of them. Okay, so it's just say just contain all of them. So that's a good exercise and, and a great understanding of buckets here. I hope you enjoyed it. Happy coding!
More from Happy

Design System Interview - Design System for a kanban board , Fullstack developer, 2024










