Today, I was asked in an interview to build a data structure as follows:
Let there be some elements and some groups. Each element associated to 'exactly 1' group has a score. The data structure must support the following operations:
insert(el_id,grp_id,x)
: Insert element with idel_id
with a scorex
to group with group_idgrp_id
set(el_id,x)
: change the score of element with idel_id
tox
.set(grp_id,x)
: change the score of all elements in the group with idgrp_id
tox
.print(grp_id)
: print the max score element's id in that group. (Return any if multiple exist)
Constraints:
1 <= no_of_elements <= 1e6
1 <= no_of_groups <= 5
1 <= score <= 5
I couldn't solve it during the interview and also couldn't think of any solution later. Would someone please help?