О чём разговор
Weighted random selection, binary search, complexity, Event DB team, CI/CD, Scrum
Этапы
- 1Техническое
Все вопросы
- 00:00
How would you select a random country so that the probability of each country being selected is proportional to its population?
Код - 06:44
So what would actually be the contents of that array?
Код - 12:15
So in general, what is the size of the array?
Код - 14:15
Once you have this random number, how do you select the correct country?
Код - 15:59
But then you can't really take advantage of the map's lookup complexity, right, because the intervals store only the lowest and highest element?
Код - 17:35
So what is the complexity of the random selection in this case?
Код - 19:24
And the setup step costs k, right?
Код - 20:22
Is there a solution that can do the random selection in constant time?
Код - 21:22
Can you optimize the average complexity of the random selection without changing the asymptotic complexity?
Код - 23:04
By doing this you are making the asymptotic complexity worse, right, so can you somehow optimize the variant with the binary search?
Код - 24:36
Where should the binary search start to be optimal, and can you do something about the ordering?
Код - 25:05
And then the second biggest country would be where?
Код - 32:46
Do you know what would be the next steps after this interview?
О компании
Ключевые моменты
- 00:00
Problem: random country selection by population
- 17:35
Complexity: setup O(k), selection O(log k)
- 20:22
Question: constant-time selection
- 23:04
Speeding up average time with binary search
- 25:48
Switch from the problem to team questions
- 32:46
Next steps: HackerRank and hiring-manager interview
Отрывок из разговора
yeah right that's an option but by doing this you are making the asymptotic complexity worse right because in the worst case you will select the smallest country which will be at the end so in some cases you have to walk the whole array can
Yeah, I remember even some university trainings where we were adding some mathematical formulas there. But for this particular case... So let me start to reason. So the normal binary search starts at the middle, And then it checks for the n
Yeah. Well, the binary search should start in the middle, if it should be, like, optimal. But maybe you can do something about the ordering, right?
Mm-hmm. Well, if we are standing here at the middle, we should have the biggest country in the middle. So that would be the simplest optimization.
Sounds good. And then the second biggest country would be where?
um either to the left or to the right i guess the answer would be yeah i think it doesn't i do not see the reason why we pick left or right here so randomly
Что вынести
A technical interview for a Java developer role on the Event DB team. The interviewer gave a weighted random selection problem; the candidate proposed storing intervals and searching them with binary search, analysed the complexity, and discussed how to speed up the average case. The second half covered the team, its processes and the next steps; the candidate was rejected.
Разбор собран по записи разговора.
Расшифровка
problem and then we can discuss some possible solutions how we would approach it we don't need to arrive at the perfect solution right away because we can discuss several of them and discuss them from the point of runtime complexity space complexity and so on so yeah the task is that you are given two arrays one array holds country names The other array at the corresponding indexes holds population of that country.
Okay.
Right. So it's kind of a mapping from country name to country population. And now your goal is to select a random country from that list. But you don't want to select each country with the same probability. but you kind of need the probability of the country being selected to be proportional to the population of that country. So in other words, the countries with the biggest populations will be selected most frequently, while the smallest countries will not be selected so often.
Should I share IntelliJ? Is this live coding?
or i just uh it's not coding uh like you can use some i don't know if actually zoom has some whiteboard or something like that like you don't need to code anything you just need to describe your approach how you would think about that and how you would do the random selection basically you can imagine that for the random selection you have at your disposal like typical random generators so you can generate like floating point numbers in any range or integer numbers in any range but your goal is kind of to implement the weighted
probability. Okay so I have two arrays country names and population. So I suppose those are two data structures and each of them should have key. So it's like you could imagine this as a hash map, right? The key would be the country and the valley's population.
Yeah, exactly.
Okay. And it's like a random country with the population. based on the... with the weight based on the population.
Yes, that's right.
So if I try to come up with an example, let's say we have USA, which has, I don't know, just a random number, one million population, and then we have Czech Republic, which has half million population. In that case, the probability should be distributed based on one to two.
That's right.
I know that generally when we are dealing with such a problem... Well, to pick a random number in Java, it's quite easy to have a random class. so and I should probably stay language agnostic at this point. The solution should be to kind of display this weight as represented in an array basically. So if you have in this first example, if you have... yeah, it would be nice to have some charts here. Yeah, exactly. Maybe I can open Excalibur or Paint and just share it. Yeah, let's... Okay.
Let me know if you see it. Not yet. Yes, I see it now.
So, yeah, the example is USA. Oh, let me make it... I haven't used PIN for a while. So, USA would be 1 million. Oh, sorry. I should use text. and should be half median. Or just to stay with the integers, let's keep it this way. In that case, normally it would be an array with two entries, where... or maybe let me paint it already like this. So we should basically, based on this information, based on this input, we should generate an array that has as the length represented as those numbers, as those population numbers. In that case, that would practically mean that this is array of three elements, or we could think of it this way.
Дальше — после входа. Это бесплатно и займёт минуту.
Войти и читатьПохожие собеседования
VK
Java Developer
LRU-кэш, HashMap и ConcurrentHashMap, deadlock, Atomic и серии одинаковых символов
Напиши функцию: для каждого уникального символа строки верни максимальное число его непрерывных повторений подряд.
СОГАЗ
Java Developer
Car model data classes, defect filter by year, remote service mocks, grouping by model
Could you elaborate on what you mean by not exposing?
Oradian
Java Developer
Stock reservation race, JDBC batch import, optimistic locking and retries
What are the two failing tests doing, and why are they failing?
Сбер
Java Developer
WebFlux и Reactor, Kafka: порядок и дубли, Spring beans, многопоточность
Почему вы хотите уйти с последнего места работы?