Priority Queue solved

The problem context involves managing a list of students where each student has a unique token, a name, and a CGPA. The students are added to a priority queue when an 'ENTER' command is given, and removed from the queue when a 'SERVED' command is given. The priority of students in the queue is determined first by their CGPA, then by their name, and finally by their token.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Priority Queue solved HackerRank solution using CodeRankGPT:

The solution approach involves creating a 'Student' class to hold the student data and a custom comparator to manage the priority queue. The comparator first compares the CGPA of the students. If the CGPA is the same, it compares the names. If the names are also the same, it compares the tokens. The 'main' function reads commands from the input, performs the 'ENTER' and 'SERVE' operations on the priority queue, and prints the names of the students in the queue in the order they would be served.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.datastructures;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 *
 */


public class JavaPriorityQueue {
	static class Student {
		private int token;
		private String fname;
		private double cgpa;

		public Student(int id, String fname, double cgpa) {
			super();
			this.token = id;
			this.fname = fname;
			this.cgpa = cgpa;
		}

		public int getToken() {
			return token;
		}

		public String getFname() {
			return fname;
		}

		public double getCgpa() {
			return cgpa;
		}
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		PriorityQueue data = new PriorityQueue(new Comparator() {
			@Override
			public int compare(Student o1, Student o2) {
				if (o1.getCgpa() < o2.getCgpa()) {
					return 1;
				} else if (o1.getCgpa() > o2.getCgpa()) {
					return -1;
				} else {
					if (o1.getFname().compareTo(o2.getFname()) == 0) {
						if (o1.getToken() > o2.getToken()) {
							return 1;
						} else if (o1.getToken() < o2.getToken()) {
							return -1;
						} else {
							return 0;
						}

					} else {
						return o1.getFname().compareTo(o2.getFname());
					}
				}
			}
		});
		for (int i = 0; i < t; i++) {
			String op = sc.next();
			switch (op) {
			case "ENTER":
				String name = sc.next();
				double cgpa = sc.nextDouble();
				int id = sc.nextInt();
				Student s = new Student(id, name, cgpa);
				data.add(s);
				break;
			case "SERVED":
				if (data.isEmpty()) {
					break;
				}
				data.remove();

			}
		}
		if (data.isEmpty())
			System.out.println("EMPTY");
		else {
			while (!data.isEmpty()) {
				Student st = data.poll();
				System.out.println(st.getFname());
			}
		}
		sc.close();
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform