Goldman Sachs Interview Question for Software Engineer / Developers






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

using namespace std;
typedef map<string,double> strmap;	

void map_diff(strmap& map1,strmap& map2,strmap& result){

	strmap::const_iterator it1 , it2;
	it1 = map1.begin();
	while(it1 != map1.end()){
		result[(*it1).first] = (map2[(*it1).first] - (*it1).second);
		it1++;		
	}
}
int main( )
{
	strmap map1 , map2 , result;
	map1["sac"] = 10.00;
	map1["in"] = 50.00;
	map1["an"] = 100.00;
	map1["anu"] = 600.00;
	map1["sj"] = 90.00;
	map1["rbv"] = 10.89;

	map2 = map1;
	map_diff(map1,map2,result);

	for(strmap::const_iterator it = result.begin(); it !=  result.end(); it++){
	
		cout << (*it).first << " " << (*it).second << endl;
	}
}

- sachin323 March 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Nice 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?

- xankar March 09, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Shalini March 10, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

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

}

- Lakshmanan Venkatachalam March 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the problem clearly states that the NEW data structure should have the difference between correspondent keys. I that case case i believe the best approach for entries that were not present in both original tables would be returning an error or throwing an exception.

- Manox March 24, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

}

- ABC May 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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

- Madhur January 09, 2015 | Flag Reply


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