Loading...
Loading...
A register keeps students in a hash table with 8 buckets, and a student is identified by the roll number: equals() and hashCode() (Objects.hash(roll)) use only the roll. When a roll number changes, the student must be taken out of the old bucket, changed, and filed again, or it is lost. Handle the commands and print the result of each.
ADD roll name, CHANGE old new or FIND roll.Added <name>, or Roll <roll> taken. CHANGE: Moved <name> to <new>, Roll <old> not found, or Roll <new> taken when another student has it. FIND: the name, or Roll <roll> not found.Example
Input
7 ADD 14 Kavya ADD 15 Rohan CHANGE 14 15 CHANGE 14 16 FIND 14 FIND 16 ADD 14 Arjun
Output
Added Kavya Added Rohan Roll 15 taken Moved Kavya to 16 Roll 14 not found Kavya Added Arjun
After Kavya moves to 16, roll 14 is free again.
Try the problem first. Open one hint at a time.
Write find(roll): make a probe new Student(roll, ""), go to its bucket and compare with equals().
For CHANGE: remove the student from its current bucket first, then change the roll, then add it again.
To know whether the new roll is "taken by another student", compare the holder with !=: here identity is exactly the question.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor