Goldman Sachs Interview Question
Software Engineer / DevelopersNice so far. In your code, you assigned map2= map1 for duplication, What if keys in map1 and map2 have some different keys? Like create map1 and map2 such that the key are map1_keys={john, chethan, flick, akkan} and map2_keys={john, twitter, flick,dan}, then you will have to iterate over map2 too, right?
How can you combine iterating both map1 and map2 efficiently and finally finding out the difference?
import java.util.HashMap;
import java.util.Iterator;
/*
* Goldman Sachs:
* There are two hashmaps with string as keys and double as values. Construct a
* method which takes in these two hashmaps and returns another data structure
* which returns difference between the values with corresponding keys.
*/
public class HashMapProgram{
public static void main(String args[]){
HashMap<String, Integer> map1 = new HashMap<String, Integer>();
HashMap<String, Integer> map2 = new HashMap<String, Integer>();
addElements(map1,map2);
Iterator it = map1.keySet().iterator();
while(it.hasNext()){
Object key = it.next();
int value1 = map1.get(key);
if(map2.containsKey(key)){
int value2 = map2.get(key);
map1.put((String) key, value1-value2);
}
}
Iterator it3 = map2.keySet().iterator();
while(it3.hasNext()){
Object key = it3.next();
if(!map1.containsKey(key)){
map1.put((String) key,map2.get(key));
}
}
Iterator it1 = map1.keySet().iterator();
while(it1.hasNext()){
Object key = it1.next();
int value1 = map1.get(key);
System.out.println(key+":"+map1.get(key));
}
}
private static void addElements(HashMap<String, Integer> map1,
HashMap<String, Integer> map2) {
map1.put("One",10);
map1.put("Two",20);
map1.put("Three",30);
map1.put("Four",40);
map2.put("One",1);
map2.put("Two",2);
map2.put("Five",5);
}
}
package HashMapMerger;
import java.util.HashMap;
/**
*
* @author vlakshm
*/
public final class Main {
private static final HashMap<String, Double> map1 = new HashMap(4);
private static final HashMap<String, Double> map2 = new HashMap(4);
static {
map1.put("john", 15.60);
map1.put("chethan", 15.60);
map1.put("flick", 14.60);
map1.put("akkan", 15.60);
map2.put("john", 14.60);
map2.put("twitter", 15.60);
map2.put("flick", 15.60);
map2.put("dan", 15.60);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
map1.keySet().retainAll(map2.keySet());
for(String s : map1.keySet()) {
map1.put(s, map1.get(s)-map2.get(s));
}
System.out.println("MOdified Map :" + map1);
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashMapTest1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
HashMap<String, Double> map1=new HashMap<String, Double>();
HashMap<String, Double> map2=new HashMap<String, Double>();
map1.put("one",1.11);
map1.put("two",2.11);
map1.put("three",3.11);
map1.put("four",4.11);
//map1.put(null, null);
map2.put("one",1.12);
map2.put("two",2.12);
map2.put("three",3.12);
map2.put("four",4.12);
System.out.println(map1);
System.out.println(map2);
//map2.put(null, null);
HashMap<String,Double> m3=difference(map1,map2);
System.out.println(m3);
}
static HashMap<String, Double> difference(HashMap<String, Double> m1, HashMap<String, Double> m2)
{
HashMap<String,Double> m3= new HashMap<String,Double>();
Set<String> s1=m1.keySet();
// Set<String> s2=m2.keySet();
for(Object obj:s1)
{
m3.put((String)obj, m2.get(obj)-m1.get(obj));
System.out.println(m3);
}
return m3;
//return null;
}
}
HashMap<String, Double> a = new HashMap<String, Double>(4);
HashMap<String, Double> b = new HashMap<String, Double>(4);
HashMap<String, Double> result = new HashMap<String, Double>(4);
a.put("p", 11.11);
a.put("q", 12.12);
a.put("r", 13.13);
a.put("s", 14.14);
b.put("p", 21.11);
b.put("q", 22.12);
b.put("r", 23.13);
b.put("s", 24.14);
Iterator<String> ita = a.keySet().iterator();
Iterator<String> itb = b.keySet().iterator();
while(ita.hasNext())
{
String key = ita.next();
Double res = a.get(key) - b.get(key);
result.put(key, res);
}
System.out.println(result);
- sachin323 March 07, 2010