HackerRank Java- Priority Queue




There are a number of students in a school who wait to be served. Two types of events, ENTER and SERVED, can take place which are described below.

ENTER: A student with some priority enters the queue to be served.
SERVED: The student with the highest priority is served (removed) from the queue.
A unique id is assigned to each student entering the queue. The queue serves the students based on the following criteria (priority criteria):

The student having the highest Cumulative Grade Point Average (CGPA) is served first.
Any students having the same CGPA will be served by name in ascending case-sensitive alphabetical order.
Any students having the same CGPA and name will be served in ascending order of the id.
Create the following two classes:

The Student class should implement:
The constructor Student(int id, String name, double cgpa).
The method int getID() to return the id of the student.
The method String getName() to return the name of the student.
The method double getCGPA() to return the CGPA of the student.
The Priorities class should implement the method List getStudents(List events) to process all the given events and return all the students yet to be served in the priority order.



  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4. import java.util.Comparator;
  5. import java.util.PriorityQueue;
  6. class Student {
  7. private final int id;
  8. private final String name;
  9. private final double cgpa;
  10. public Student(int id, String name, double cgpa) {
  11. this.id = id;
  12. this.name = name;
  13. this.cgpa = cgpa;
  14. }
  15. public int getID() {
  16. return id;
  17. }
  18. public String getName() {
  19. return name;
  20. }
  21. public double getCGPA() {
  22. return cgpa;
  23. }
  24. }
  25. class Priorities {
  26. private final PriorityQueue<Student> queue = new PriorityQueue<>(
  27. Comparator.comparing(Student::getCGPA).reversed()
  28. .thenComparing(Student::getName)
  29. .thenComparing(Student::getID));
  30. public List<Student> getStudents(List<String> events) {
  31. events.forEach((event) -> {
  32. if (event.equals("SERVED")) {
  33. queue.poll();
  34. } else {
  35. String[] details = event.split(" ");
  36. queue.add(new Student(Integer.parseInt(details[3]), details[1], Double.parseDouble(details[2])));
  37. }
  38. });
  39. List<Student> students = new ArrayList<>();
  40. while (!queue.isEmpty()) {
  41. students.add(queue.poll());
  42. }
  43. return students;
  44. }
  45. }
  46. public class Solution {
  47. private final static Scanner scan = new Scanner(System.in);
  48. private final static Priorities priorities = new Priorities();
  49. public static void main(String[] args) {
  50. int totalEvents = Integer.parseInt(scan.nextLine());
  51. List<String> events = new ArrayList<>();
  52. while (totalEvents-- != 0) {
  53. String event = scan.nextLine();
  54. events.add(event);
  55. }
  56. List<Student> students = priorities.getStudents(events);
  57. if (students.isEmpty()) {
  58. System.out.println("EMPTY");
  59. } else {
  60. for (Student st: students) {
  61. System.out.println(st.getName());
  62. }
  63. }
  64. }
  65. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube