🚀 Solved: Java Priority Queue Challenge (HackerRank) Today I worked on an interesting problem involving PriorityQueue in Java, where students are served based on multiple priority conditions: 🎯 Priority Rules: ✔ Highest CGPA first ✔ If CGPA is same → Name in ascending order ✔ If both same → Lower ID first 💡 What I learned: 🔹 How to use Comparator with PriorityQueue 🔹 Handling multiple sorting conditions 🔹 Difference between writing code in Eclipse vs HackerRank 🔹 Importance of using the correct constructor: PriorityQueue<Student> pq = new PriorityQueue<>(1, new Comparator<Student>() { public int compare(Student s1, Student s2) { if (s1.getCGPA() != s2.getCGPA()) { return Double.compare(s2.getCGPA(), s1.getCGPA()); } else if (!s1.getName().equals(s2.getName())) { return s1.getName().compareTo(s2.getName()); } else { return s1.getID() - s2.getID(); } } }); ⚠️ Key Debugging Insight: I initially faced an error: 👉 “no suitable constructor found for PriorityQueue” ✔ The fix was to provide initial capacity along with Comparator, especially in platforms like HackerRank. 📌 Takeaway: PriorityQueue is powerful, but understanding custom ordering logic and environment differences is crucial. #Java #DataStructures #PriorityQueue #HackerRank #ProblemSolving #JavaDeveloper #CodingJourney #Learning
Java PriorityQueue Challenge Solved with Custom Comparator
More Relevant Posts
-
Day 33 of Learning Java Today I learned about Return Types in Java methods, and it finally started to make sense how methods give results back! Here’s what I understood: 🔹 Every method has a return type 🔹 It tells what kind of value the method will give back 🔹 There are mainly two types: Primitive Data Types (PDT) : • byte • short • int • long • char • String • float • double • boolean Reference Data Types (RDT) : • Arrays • Classes • Interfaces • Annotations • Enums 🔹 A method can also return an object 🔹 The "return" keyword is used to send the value back 🔹 If nothing is returned, we use "void" Thanks to my mentor Ashim Prem Mahto for the clear explanations and for always clearing my doubts. #Java #LearningJava #ProgrammingJourney #CodingLife #JavaBasics #SoftwareDevelopment #DeveloperJourney #TechLearning #StudentLife
To view or add a comment, sign in
-
-
📘 Day 43 of My Learning Journey Today, I learned about the equals() method from the java.lang package in Java. 🔹 The equals() method is used to compare two objects for equality. 🔹 By default, it checks whether both objects refer to the same memory location. 💡 But the real power comes when we override equals() to compare object values instead of references. 👉 Example: Two objects with the same data (like two students with the same ID) can be treated as equal using equals(). 🔸 This concept is very important when working with collections like lists, sets, and maps. 🚀 Understanding equals() helps in writing accurate comparisons and avoiding logical errors in Java programs. Step by step, improving my Java skills and writing better code! 💻 #Java #LearningJourney #Day43 #OOP #Programming #TechSkills
To view or add a comment, sign in
-
-
Day 37 of Learning Java Today I learned something interesting Illegal Forward Reference in Java. At first, it sounded complicated, but once I understood it, it actually made a lot of sense! Here’s what I learned: 🔹 What is Illegal Forward Reference? • It happens when you try to use a variable before it is declared. • Java doesn’t allow referencing a variable that comes later in the code. 🔹 Why does it happen? • Java reads code from top to bottom. • If a variable is used before it exists, the compiler throws an error. 🔹 Example of the issue: • Using a variable before declaring & defining it to a compile-time error. 🔹 How to fix it? • Always declare variables before using them. • we have to call static variable using ClassName.VarName. Thanks to my mentor Ashim Prem Mahto for the clear explanations and for always clearing my doubts. #Java #LearningJava #CodingJourney #Programming #DeveloperLife #CodeNewbie #JavaDeveloper #TechLearning #StudentLife #jvm
To view or add a comment, sign in
-
-
Day 39 of learning java Today I learned something very important in Java, Object Creation. Syntax: "className objectName = new constructor();" Here’s what I understood: • The left side ("className objectName") is just declaring a reference variable. • The right side ("new constructor()") is where the actual object is created. • Memory is allocated only when we use the "new" keyword. • The constructor gets executed automatically when the object is created. • Without "new", no memory is allocated and no constructor runs. In short: Declaration != Object creation You need "new" to actually create and use the object. This concept made things much clear about how Java handles memory and execution internally. Thanks to my mentor Ashim Prem Mahto for the clear explanations and for always clearing my doubts. #Java #LearningJourney #Programming #JavaBasics #CodingLife #DeveloperJourney #TechLearning #Beginners #CodeNewbie #jvm #SoftwareEngineer #StudentLife
To view or add a comment, sign in
-
-
📅 Day 45 of My Learning Journey Today, I explored two important concepts in Java: String Class and StringBuffer. 🔹 String Class Strings are immutable, meaning once an object is created, it cannot be changed. Any modification results in the creation of a new object. Widely used for safe and secure data handling. 🔹 StringBuffer StringBuffer is mutable, meaning it allows changes without creating new objects. It is thread-safe (synchronized), making it suitable for multi-threaded environments. Provides methods like append(), insert(), and reverse() for efficient string manipulation. 💡 Key Takeaway: Use String when data should remain constant, and StringBuffer when frequent modifications are required, especially in multi-threaded applications. 📌 Understanding the difference between immutability and mutability helps in writing optimized and efficient Java programs! #Day45 #Java #StringClass #StringBuffer #Programming #LearningJourney #TechSkills
To view or add a comment, sign in
-
-
Day 38 – 44 of my Frontlines EduTech (FLM) AI-Powered Java Full Stack Journey Day 38: Started learning Collections in Java. Focused on ArrayList and how it stores dynamic data. Understood how it is different from arrays. Day 39: Learned about Iterator. Used it to traverse elements in collections. It made looping through data more clean and flexible. Day 40: Explored Set interface. Learned that it stores only unique elements. Good for removing duplicates from data. Day 41: Learned Map in Java. Stores data in key-value pairs. Very useful for real-world applications. Day 42: Covered Enum and Command Line Arguments. Also learned Static and Instance Blocks. Understood when and how they are executed. Day 43: Learned Clone, Comparator, and Comparable. Used for copying objects and sorting data. Important for customizing sorting logic. Day 44: Solved problem on frequency of characters. Used logic with collections to count occurrences. Good practice for improving problem-solving skills. Consistent learning, step by step. Fayaz S 🔗 Github: https://lnkd.in/gV_uis3J #Java #Collections #CodingJourney #100DaysOfCode #LearnJava #FullStack 🚀
To view or add a comment, sign in
-
-
🚀 Day 48 of My Java Learning Journey Today, I explored one of the most important concepts in Java – Collection Framework 💡 📌 What I Learned: Collection Framework is a set of classes and interfaces used to store and manipulate data efficiently It was introduced in JDK 1.2 by Josh Bloch Acts as an alternative to Data Structures in Java 📊 Key Components: Interfaces → List, Set, Queue, Map Classes → ArrayList, LinkedList, HashSet, HashMap 🔥 Why Collections? ✔ Dynamic size (no fixed length like arrays) ✔ Inbuilt methods (add, remove, etc.) ✔ Efficient data handling ✔ Reduces coding effort 💻 Key Features: Allows heterogeneous data Maintains insertion order (List) Allows duplicates (List) Improves performance in real-world applications 📈 Takeaway: Collection Framework is a must-know concept for interviews and real-time development. Almost every Java application uses it! #Day48 #Java #CollectionsFramework #JavaDeveloper #LearningJourney #Coding #Programming
To view or add a comment, sign in
-
-
🚀 Day 44 of My Learning Journey Today I explored some important concepts in Java related to memory management and threading. Here’s what I learned: 🔹 Finalize Method Used before an object is garbage collected. Helps in performing cleanup activities like closing resources. However, it's not reliable and is now considered outdated in modern Java practices. 🔹 Mark and Sweep Algorithm A key technique used in Garbage Collection. Mark Phase: Identifies objects that are still in use. Sweep Phase: Removes unused objects from memory. Improves memory efficiency and prevents memory leaks. 🔹 Garbage Collector Automatically manages memory by removing unused objects. Helps developers focus more on logic rather than memory handling. Works in the background for better performance. 🔹 Daemon Thread A low-priority thread that runs in the background. Supports main threads (e.g., garbage collection). Automatically stops when all user threads finish execution. 💡 Key Takeaway: Understanding how memory is managed and how background threads work is crucial for writing efficient and optimized Java programs. #Java #GarbageCollection #Multithreading #LearningJourney #Programming #Developer
To view or add a comment, sign in
-
-
Exploring one of the most powerful concepts in Java — Polymorphism, and I achieved it using Inheritance with a simple Plane program. In Java, polymorphism allows a single object to take multiple forms. Using inheritance and method overriding, I implemented a Plane example where different types of planes (like Cargo Plane and Passenger Plane) show different behaviors even though they share a common parent class. It was really interesting to see how a parent class reference can call different implementations at runtime — making the program dynamic and flexible. A big thank you to TAP Academy for teaching this concept so clearly and effortlessly. The real-time examples, like the Plane program, made it much easier to understand how inheritance and polymorphism work together. Excited to apply these concepts in real-world projects and keep growing 🚀 #Java #OOP #Polymorphism #Inheritance #CodingJourney #Learning #SoftwareDevelopment #TAPAcademy
To view or add a comment, sign in
-
🚀 Day 8 – Understanding Functions and Parameters in Java Today, I learned about functions (methods) in Java, which are very important for writing clean and reusable code. A function is simply a block of code that performs a specific task and can be used multiple times in a program. This helps to reduce repetition and makes the code easier to understand. I started by learning the basic syntax of a function, where we define a return type, function name, and body. Then I moved to functions with parameters, where values are passed into the function to perform operations. This made the concept more practical. Next, I learned about types of parameters: Formal Parameters: These are variables defined in the function. Actual Parameters: These are the values passed when calling the function. 👉 Understanding this difference made it clear how data flows inside a program. Overall, today’s learning helped me understand how to write better and more structured code using functions. 💪 I will keep practicing daily and improve step by step in my coding journey. #Java #Coding #DSA #Learning #Consistency
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development