Design data structure for players and ranks

Given a list of player names and their scores, design a data structure that can support following modules in optimal time- updateEntry(String name) – Increase the player score with “name” by 1. If no player with name is present then add a player with score 1. getEntryFromRank(int rank) – Get the players with the given … Read more

Stack Java Class – Explained

Earlier we saw about Stack and its implementation using Linked List. Java has a built in class for Stack. In this article we will discuss about it in detail. First brief about Stack. What is Stack?? Stack is an abstract data type (ADT) and very useful in programming. In computer science, a stack is an abstract data type that serves … Read more

Calculate Logn base r – Java Implementation

Objective: Given a number n and r, write a program to calculate Logrn Example: N = 32, r =2 Log232= 5 N = 64, r = 4 Log464= 3 Approach: Without using built-In function- Initialize result = 0. Keep dividing the given number by r till number is greater than 0 and add one to … Read more

Print My IP Address – Java Code

Objective: Write a java program to print your IP address. Approach: Get the local host address of your system using getLocalHost() method. Use getHostAddress() method to get the IP address from local host address. Code:Run This Code import; import; public class PrintIPAddress { public static void getIPAddress(){ try { //get the local host InetAddress localHost = … Read more

Check if two Strings are equal without using built-in function – Java

Objective– Given two strings, find out if they are equal or not without using any built-in function (without using equals() function in java). Example: String x=’tutorial’ and String y=’tutorial’ are equal – true String x=’tutorial’ and String y=’tutorial ‘ are equal – false String x=’tutorial’ and String y=’ ‘ are equal – false Approach: If … Read more

Java Pair Class

Class Pair<Key, Value> – A convenience class to represent name-value pairs. pair stores a key-pair value. Two Pair objects are considered equal if key and value of one pair is matching with second key. This class also generates hash code using key and value. So hash code will also be same for two Pair objects … Read more

Deque Implementation – Java

Java Deque Interface – It’s a linear collection. The Deque interface is a subtype of the util.Queue interface. Deque is acronym of “Double Ended Queue” means it supports insertion and removal of data from both the ends. So it can be used as Stack OR Queue. Declaration public interface Deque extends Queue Methods: Return Type Method Description boolean add(E e)  Insert … Read more

Priority Queue in Data Structure

Earlier in we have seen Min-Heap and Max-Heap Implementation. Priority Queue is its built-in implementation in Java. In this article we will see how to perform Min-Heap and Max-Heap using Priority Queue. Brief: A priority queue is an abstract data type where each element has a “priority” assigned to it. So the element with the … Read more