The problem context involves creating a phone directory where each entry consists of a name and a phone number. The directory should be able to handle queries, returning the phone number for a given name if it exists in the directory, or 'Not found' if it doesn't.
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 🥷
The solution approach involves using a HashMap data structure in Java, where the name is the key and the phone number is the value. The program reads the number of entries from the user, then for each entry, it reads the name and phone number and adds them to the HashMap. It then enters a loop where it reads queries from the user. For each query, it checks if the name exists in the HashMap. If it does, it prints the name and phone number. If it doesn't, it prints 'Not found'.
/**
*
*/
package com.javaaid.hackerrank.solutions.languages.java.datastructures;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
*
*/
public class JavaMap {
public static void main(String[] argh) {
Scanner in = new Scanner(System.in);
Map hmap = new HashMap();
int n = in.nextInt();
in.nextLine();
for (int i = 0; i < n; i++) {
String name = in.nextLine();
int phone = in.nextInt();
hmap.put(name, phone);
in.nextLine();
}
while (in.hasNext()) {
String s = in.nextLine();
if (hmap.containsKey(s)) {
System.out.println(s + "=" + hmap.get(s));
} else {
System.out.println("Not found");
}
}
in.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. 🧐
The form has been successfully submitted.