Google Interview Question


Country: United States
Interview Type: Phone Interview




Comment hidden because of low score. Click to expand.
4
of 6 vote

Create a class that maintains two HashMap members. One member is to store <K, V> pair and the other is for <V, K> pair.

- avondale May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Excellent solution. I would ask the interviewer about whether uniqueness of values is guaranteed, etc., but other than that, this is an efficient and elegant solution.

- eugene.yarovoi May 03, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What to do if we dont have unique values??

- Puneet August 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

If you declare 2 Hash map then question is trivial and doesn't make sense for Google Interview.

- Andy2000 September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Well we can have a single map and while inserting

Map.put(key,value)
Map.put(value,key)

The only constraint is all the keys and the values should b unique.

- Ramindar June 15, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is a strong solution. You can make the class inherit from HashMap and just override get and put methods

- dchang.me September 04, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

package Maps;

import java.util.HashMap;

public class BiDirMap<K,V> {
private HashMap<KeyValWrapper<K,V>, KeyValWrapper<K,V>> keyToValMap;
private HashMap<KeyValWrapper<K,V>, KeyValWrapper<K,V>> valToKeyMap;

enum keyOrVal {
KEY, VALUE
}

public BiDirMap (int initialCapacity) {
keyToValMap = new HashMap<KeyValWrapper<K,V>, KeyValWrapper<K,V>>();
valToKeyMap = new HashMap<KeyValWrapper<K,V>, KeyValWrapper<K,V>>();
}

public V getVal (K key) {
KeyValWrapper wrapper = new KeyValWrapper(key);
KeyValWrapper<K, V> retkey = keyToValMap.get(wrapper);
if (retkey != null) {
return retkey.getVal();
} else {
return null;
}
}

public K getKey (V val) {
KeyValWrapper wrapper = new KeyValWrapper(val);
KeyValWrapper<K, V> retKey = valToKeyMap.get(wrapper);
if (retKey != null) {
return retKey.getKey();
} else {
return null;
}
}

public void put (K key, V value) {
KeyValWrapper kWrapper = new KeyValWrapper(key);
kWrapper.setVal(value);
KeyValWrapper vWrapper = new KeyValWrapper(value);
vWrapper.setVal(key);
keyToValMap.put (kWrapper, kWrapper);
valToKeyMap.put (vWrapper, vWrapper);
}

private static class KeyValWrapper<K,V> {
K key;
V value;

public KeyValWrapper (K kv) {
this.key = kv;
}

public void setVal (V value) {
this.value = value;
}

public V getVal() {
return value;
}

public K getKey() {
return key;
}

public boolean equals (Object wrap) {
KeyValWrapper<K, V> wrapper = (KeyValWrapper<K, V>) wrap;
if (this.key.equals(wrapper.getKey())) {
return true;
} else {
return false;
}
}

public int hashCode () {
return key.hashCode();
}
}
}

- Anonymous May 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Mapp extends HashMap {

Vector arKey = new Vector();
Vector arval = new Vector();

@Override
public Object put(Object key,Object value){
arKey.add(key);
arval.add(value);
return super.put(key,value);
}


public Object getFromValue(Object key){
return arKey.get(arval.indexOf(key));
}

@Override
public Object get(Object key){
return super.get(key);
}
}

- lohith May 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think you should be more careful while using Vector in this case.

- GingerBreadMan May 21, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class BiDirectionalHexMap<T, U> extends HashMap<T, U>
{

    private static final long serialVersionUID = 1L;
    private HashMap<U, List<T>> reverse = new HashMap<U, List<T>>();

    void populateReverse()
    {
        Set<T> kset = keySet();
        Iterator<T> itr = kset.iterator();
        while (itr.hasNext())
        {
            T key = itr.next();
            U val = get(key);
            if (reverse.containsKey(val))
            {
                reverse.get(val).add(key);
            }
            else
            {
                ArrayList<T> x = new ArrayList<T>();
                x.add(key);
                reverse.put(val, x);
            }
        }
    }
    
    public List<T> getKeys(U value)
    {
        populateReverse();
        return reverse.get(value);

    }

    public static void main(String[] args)
    {
        BiDirectionalHexMap<String, String> map = new BiDirectionalHexMap<String, String>();
        map.put("key1", "val");
        map.put("key2", "val");
        System.out.println(map.getKeys("val"));
        System.out.println(map.get("key1"));
    }
}

- Anonymous May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.) If space is constraint: Use a 2D array to store the key-value pair.
2.) If time is constraint: Use a data structure which encapsulates 2 hashmaps <K,V> and <V,K>

- anujkrmodi July 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Given a map, searching of element in O(n) cannot be considered. So, having 2D array is bad idea.

- Harsh August 18, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Create two arrays and one hash function. One array stores the key, the other stores the value. When adding the pair to bihash, store the key in the same position of the array as the value. During query, use hash function to get the position of the array, and return the value from the value array if it's finding value to a key and vice versa.

- Daisy G May 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what happens if the value type is integer... how would you add the pair?

- me January 30, 2014 | 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