Goldman Sachs Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
String [][] pDirArr = {{"abc", "123"},{"Abc", "1234"}, {"ABC", "789"},{"xy", "123"},{"XY", "456"}};
Map<String,String> inputDirectoryMap = new HashMap<String,String>();
for(int i=0;i<pDirArr.length;i++){
String name=(pDirArr[i][0]).toLowerCase();
if(inputDirectoryMap.containsKey(name)){
String previousNum= inputDirectoryMap.get(name);
inputDirectoryMap.put(name,previousNum+" , "+pDirArr[i][1]);
}else{
inputDirectoryMap.put(name,pDirArr[i][1]);
}
}
for (Map.Entry<String,String> entry : inputDirectoryMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
import java.util.*;
import java.lang.*;
import java.io.*;
public class HelloWorld {
public static void printRepeatedWord(String a)
{
Map<String, String> num = new HashMap<String, String>();
num.put("ABc","123");
num.put("bcd","345");
num.put("cda","523");
num.put("abc","678");
Set ab = num.keySet();
for(Object abdd:ab) {
if((abdd.toString().toLowerCase()).equals(a.toLowerCase())) {
System.out.println(num.get(abdd.toString()));
}
}
}
public static void main(String[] args) {
String a ="Abc";
printRepeatedWord(a);
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Phone {
public static void main(String [] args)
{
String str = null;
String quest=null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Map<String, List<Integer>> hmap = new HashMap<String, List<Integer>>();
try {
while((str=br.readLine()) != null)
{
String [] tokens;
tokens=str.split("---");
if(tokens.length==1)
{
quest=tokens[0];
System.out.println(quest);
break;
}
else
{
if(hmap.containsKey(tokens[0].toLowerCase()))
{
List<Integer> hl=hmap.get(tokens[0].toLowerCase());
hl.add(Integer.parseInt(tokens[1]));
}
else
{
List<Integer> hl = new ArrayList<>();
hl.add(Integer.parseInt(tokens[1]));
hmap.put(tokens[0].toLowerCase(),hl);
}
}
System.out.println(hmap);
}
} catch (NumberFormatException | IOException e1) {
e1.printStackTrace();
}
for(Entry<String, List<Integer>> e:hmap.entrySet())
{
if(e.getKey().equals(quest.toString()))
{
System.out.println(e.getValue().toString());
}
}
}
}
import java.util.*;
public class PhoneDirectory {
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("ABC",1234);
map.put("abc", 4567);
map.put("Abc", 879);
Set key=map.keySet();
Iterator it=key.iterator();
System.out.println(key);
while(it.hasNext())
{
String str=it.next().toString();
String str1=str.toLowerCase();
//System.out.println(str);
if(str1.equals("abc"))
{
System.out.println(map.get(str));
}
}
}}
package algo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class CounterAlgo2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> strList = null;
Entry entry = null;
String [][] pDirArr = {{"abc", "123"},{"Abc", "1234"}, {"ABC", "789"},{"xy", "123"},{"XY", "456"}};
Map<String, List<String>> pDir = new HashMap<String, List<String>>();
for(String[] strarr : pDirArr){
String key = strarr[0].toLowerCase();
strList = pDir.get(key);
if(strList == null){
strList = new ArrayList<String>();
strList.add(strarr[1]);
pDir.put(key, strList);
}else{
strList.add(strarr[1]);
}
}
Iterator<Entry<String, List<String>>> it = pDir.entrySet().iterator();
while(it.hasNext()){
entry = it.next();
System.out.println(entry.getKey()+"="+entry.getValue().toString());
}
}
}
public class GetPhoneNumber {
Hashtable<String, Integer> hashtable = new Hashtable<>();
public void getPhoneNumber(String personName) {
hashtable.put("ABc", 123);
hashtable.put("bcd", 345);
hashtable.put("cda", 523);
hashtable.put("abc", 678);
Set<String> keys = hashtable.keySet();
for(String key : keys) {
if(key.equalsIgnoreCase(personName)) {
System.out.println(hashtable.get(key));
}
}
}
}
public class GetPersonPhone {
Hashtable<String, Integer> hashtable = new Hashtable<>();
public void getPhoneNumber(String personName) {
hashtable.put("ABc", 123);
hashtable.put("bcd", 345);
hashtable.put("cda", 523);
hashtable.put("abc", 678);
Set<String> keys = hashtable.keySet();
for(String key : keys) {
if(key.equalsIgnoreCase(personName)) {
System.out.println(hashtable.get(key));
}
}
}
}
public class GetPersonPhone {
Hashtable<String, Integer> hashtable = new Hashtable<>();
public void getPhoneNumber(String personName) {
hashtable.put("ABc", 123);
hashtable.put("bcd", 345);
hashtable.put("cda", 523);
hashtable.put("abc", 678);
Set<String> keys = hashtable.keySet();
for(String key : keys) {
if(key.equalsIgnoreCase(personName)) {
System.out.println(hashtable.get(key));
}
}
}
}
public class GoldManPhone {
Hashtable<String, Integer> hashtable = new Hashtable<>();
public void getPhoneNumber(String personName) {
hashtable.put("ABc", 123);
hashtable.put("bcd", 345);
hashtable.put("cda", 523);
hashtable.put("abc", 678);
Set<String> keys = hashtable.keySet();
for(String key : keys) {
if(key.equalsIgnoreCase(personName)) {
System.out.println(hashtable.get(key));
}
}
}
}
public static void getPhoneNumbers(String str) {
Map<String, Integer> h = new TreeMap<String, Integer>();
h.put("Abc", 123);
h.put("Abd", 123);
h.put("Ade", 123);
h.put("abc", 456);
for (String key : h.keySet()) {
if(key.equalsIgnoreCase(str)) {
System.out.println("Practice.getPhoneNumbers() >>"+ key + ">>" + h.get(key));
}
}
}
Contact class for key : - have overridden the equals and hashcode method accordingly
public class Contact {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode(){
return name.length();
}
public boolean equals(Object o){
if(o instanceof Contact){
Contact c = (Contact)o;
String name = this.getName();
if(c.getName().equals(name) || (c.getName().toLowerCase()).equals(name.toLowerCase())){
return true;
}
}
return false;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.getName();
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class PhoneDirectory {
static Map<Contact, List<Integer>> phoneDirectory = new HashMap<Contact, List<Integer>>();
public static void main(String[] args) {
addToDirectory("ABc", 123);
addToDirectory("bcd", 345);
addToDirectory("cba", 523);
addToDirectory("abc", 678);
Iterator<Contact> it = phoneDirectory.keySet().iterator();
while (it.hasNext()) {
Contact contact = (Contact) it.next();
System.out.println(contact +" "+phoneDirectory.get(contact));
}
Contact c = new Contact();
c.setName("abc");
System.out.print(phoneDirectory.get(c));
}
public static void addToDirectory(String name,int number){
Contact contact = new Contact();
contact.setName(name);
if(phoneDirectory.get(contact) != null){
List<Integer> numberList = phoneDirectory.get(contact);
numberList.add(number);
}else{
List<Integer> numberList = new ArrayList<Integer>();
numberList.add(number);
phoneDirectory.put(contact, numberList);
}
}
}
package com.akshay.codechef.paractice;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class TestPhonedirectory {
Map<String ,Integer > directory=null;
public static void main(String args[]) throws IOException
{
TestPhonedirectory tst= new TestPhonedirectory();
tst.fillDirectory();
BufferedReader br = new BufferedReader((new InputStreamReader(System.in)));
System.out.println("enter the key you waant to read");
String skey=br.readLine();
tst.searchElement(skey);
}
private void searchElement(String skey) {
if (skey!= null)
{
Set<String> keySet=directory.keySet();
for (String s :keySet)
{
if(s.equalsIgnoreCase(skey))
{
System.out.print(directory.get(s));
}
}
}
}
public void fillDirectory()
{
directory = new HashMap<String, Integer>();
directory.put("Abc", 123);
directory.put("BCD", 234);
directory.put("abc", 456);
}
}
String [][] pDirArr = {{"abc", "123"},{"Abc", "1234"}, {"ABC", "789"},{"xy", "123"},{"XY", "456"}};
Map<String,String> inputDirectoryMap = new HashMap<String,String>();
for(int i=0;i<pDirArr.length;i++){
String name=(pDirArr[i][0]).toLowerCase();
if(inputDirectoryMap.containsKey(name)){
String previousNum= inputDirectoryMap.get(name);
inputDirectoryMap.put(name,previousNum+" , "+pDirArr[i][1]);
}else{
inputDirectoryMap.put(name,pDirArr[i][1]);
}
}
for (Map.Entry<String,String> entry : inputDirectoryMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
String [][] pDirArr = {{"abc", "123"},{"Abc", "1234"}, {"ABC", "789"},{"xy", "123"},{"XY", "456"}};
Map<String,String> inputDirectoryMap = new HashMap<String,String>();
for(int i=0;i<pDirArr.length;i++){
String name=(pDirArr[i][0]).toLowerCase();
if(inputDirectoryMap.containsKey(name)){
String previousNum= inputDirectoryMap.get(name);
inputDirectoryMap.put(name,previousNum+" , "+pDirArr[i][1]);
}else{
inputDirectoryMap.put(name,pDirArr[i][1]);
}
}
for (Map.Entry<String,String> entry : inputDirectoryMap.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
Hey,
I am assuming the phone numbers are in some file.
I used a hash map to add names as keys and phone numbers as ArrayList.
Here is the code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
public class PhoneNumbers {
public static void main (String[] s) throws IOException{
getPhone("resources/Directory_phone.txt");
}
public static void getPhone(String str) throws IOException{
FileReader fr = new FileReader(str);
BufferedReader br = new BufferedReader(fr);
String thisLine = null;
Hashtable<String,List<String>> ht = new Hashtable<String,List<String>>();
while((thisLine = br.readLine()) != null){
//System.out.println(thisLine);
String[] nameNum = thisLine.split("--");
List<String> values;// = new ArrayList<String>();
if(ht.get(nameNum[0].toLowerCase())==null){
values = new ArrayList<String>();
}else{
values = ht.get(nameNum[0].toLowerCase());
}
values.add(nameNum[1]);
ht.put(nameNum[0].toLowerCase(), values);
}
Enumeration<String> enumKey = ht.keys();
while(enumKey.hasMoreElements()){
String s = enumKey.nextElement();
List<String> values = ht.get(s);
System.out.println(s + " -- "+ values);
}
br.close();
}
}
The text file :
abc--123
AbC--432
des--212
des--433
Home--322
Hey, are these your homeworks?!
- autoboli January 10, 2015Build a symbol table with KEY,VAL pairs. VAL can be a linked list of strings. When adding a new KEY,VAL pair into the symboil table, if the KEY is present, append VAL to already existing VAL.
At query time, given string s, return VAL for given KEY=s it the KEY exist, return null otherwise.