Accenture Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
import java.util.HashMap;
public class PhoneBook {
private static HashMap<String, String> hshmp = new HashMap<String, String>();
private static HashMap<String, String> hshmpNew = new HashMap<String, String>();
public static void main(String[] args) {
hshmp.put("8050", "name1");
hshmpNew.put("name1", "8050");
String myName = hshmp.get("8050");
String myNum = hshmpNew.get(myName);
hshmp.remove(myNum);
}
}
package com.test.me;
public class PhoneDir {
private String name = null;
private int telNumber = 0;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the telNumber
*/
public int getTelNumber() {
return telNumber;
}
/**
* @param telNumber the telNumber to set
*/
public void setTelNumber(int telNumber) {
this.telNumber = telNumber;
}
}
--------------------------------------------------------------------------------------------------------------
package com.test.me;
import java.util.Comparator;
public class PhoneComp implements Comparator<PhoneDir> {
@Override
public int compare(PhoneDir o1, PhoneDir o2) {
return (Integer.valueOf(o1.getTelNumber()).compareTo((Integer)o2.getTelNumber()));
}
}
------------------------------------------------------------------------------------------------------------------
package com.test.me;
import java.util.ArrayList;
import java.util.Collections;
public class PhoneBook {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PhoneDir phdir = new PhoneDir();
phdir.setName("Mahesh");
phdir.setTelNumber(981919128);
PhoneDir phdir1 = new PhoneDir();
phdir1.setName("Suresh");
phdir1.setTelNumber(85354348);
PhoneDir phdir2 = new PhoneDir();
phdir2.setName("ramesh");
phdir2.setTelNumber(92557665);
PhoneDir phdir3 = new PhoneDir();
phdir3.setName("sarvesh");
phdir3.setTelNumber(04545343);
PhoneDir phdir4 = new PhoneDir();
phdir4.setName("harish");
phdir4.setTelNumber(843575665);
ArrayList<PhoneDir> phoneDirList = new ArrayList<>();
phoneDirList.add(phdir);
phoneDirList.add(phdir1);
phoneDirList.add(phdir2);
phoneDirList.add(phdir3);
phoneDirList.add(phdir4);
PhoneComp comp = new PhoneComp();
Collections.sort(phoneDirList, comp);
for(PhoneDir phd: phoneDirList){
System.out.print(phd.getTelNumber());
System.out.print("\n");
}
}
}
You can do it using HashMap and an ArrayList.
iterate over hashmap entrySet and put all matching name keys in ArrayList
Then iterate over ArrayList and remove keys from map.
e.g.
Map<String,String> phoneBook = new HashMap<String, String>();
phoneBook.put("908978","PPPP");
phoneBook.put("8769797","KKKK");
phoneBook.put("7435645","YYYY");
phoneBook.put("63453254","MMMM");
phoneBook.put("89855","MMMM");
String nameToRemove = "MMMM";
List<String> numberList = new ArrayList<String>();
for(Map.Entry<String, String> entry:phoneBook.entrySet()){
if(entry.getValue().equals(nameToRemove)){
numberList.add(entry.getKey());
}
}
for(String str:numberList){
phoneBook.remove(str);
}
for(String str:phoneBook.values()){
System.out.println(str);
}
You could just use two HashMaps, one for mapping number -> name and the other for mapping name -> number. That's probably the most reasonable solution here.
I don't think you can do that, because a phone number is assigned to one person , but one person can have more than one phone number. What I am trying to say here is that a map has unique keys.
You can do that only if number to name is HashMap<String, String> and name to number is HashMap<String, ArrayList<String>>. This will work.
Basically the solution calls for a BiDirectional Map or Bidi Map
I saw this code on stackoverflow from @GETah
- vasa.v03 January 30, 2013