Accenture Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

Basically the solution calls for a BiDirectional Map or Bidi Map

I saw this code on stackoverflow from @GETah

public class BidirectionalMap<KeyType, ValueType>{
        private Map<KeyType, ValueType> keyToValueMap = new ConcurrentHashMap<KeyType, ValueType>();
        private Map<ValueType, KeyType> valueToKeyMap = new ConcurrentHashMap<ValueType, KeyType>();

        synchronized public void put(KeyType key, ValueType value){
            keyToValueMap.put(key, value);
            valueToKeyMap.put(value, key);
        }

        synchronized public ValueType removeByKey(KeyType key){
            ValueType removedValue = keyToValueMap.remove(key);
            valueToKeyMap.remove(removedValue);
            return removedValue;
        }
        synchronized public KeyType removeByValue(ValueType value){
            KeyType removedKey = valueToKeyMap.remove(value);
            keyToValueMap.remove(removedKey);
            return removedKey;
        }

        public boolean containsKey(KeyType key){
            return keyToValueMap.containsKey(key);
        }

        public boolean containsValue(ValueType value){
            return keyToValueMap.containsValue(value);
        }

        public KeyType getKey(ValueType value){
            return valueToKeyMap.get(value);
        }

        public ValueType get(KeyType key){
            return keyToValueMap.get(key);
        }
    }

- vasa.v03 January 30, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
}
}

- Ashish January 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

There is no other way out. Create a class with an embedded hashMap, searching a name by number is trivial. Deleting all numbers with name is a linear scan over the HashMap and can be implemented as an overridden method.

- Abhi January 29, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the question its been said "remove" by any chance does that mean to delete the entry ???
If that is the case using two hashmap wont work as they both will contain different data after entries removed from one of the map

- @Manpreet February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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");
}
}

}

- SkullCrusher February 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
		}

- chandra March 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ffedfdfdfdfdfdfdfdfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

- Anonymous March 04, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

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.

- eugene.yarovoi December 12, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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.

- Kamy December 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Kamy: If the phone book is such that it supports multiple numbers for a single name, then yes, we can make the simple change that you propose. I took the phrasing of the question to imply that there will only be one number associated with each name.

- eugene.yarovoi December 13, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More