Uber Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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

Thanks for giving such a good and simple example .Given below piece of code
is only checking only first value in list and then appending value at end.here you are checking if other value in list may have duplicate key. Please have a look

if(e != null) {
                  // If we will insert duplicate key-value pair,
                  // Old value will be replaced by new one.
                  if(e.key.equals(k)) {
                        e.value = v;
                  } else {
                        // Collision: insert new element at the end of list
                        // in the same bucket
                        while(e.next != null) {
                              e = e.next;
                        }
                        Entry entryInOldBucket = new Entry(k, v);
                        e.next = entryInOldBucket;
                  }
            }

Please Correct if I am wrong
code snippet will be

while(e!=null){
if(e.key.equals(key)){
e.value=value;
break;
}else{
if(e.next!=null){
e=e.next;
}else{
Entry entryInOldBucket = new Entry(k, v);
 e.next = entryInOldBucket;
break;
}

}
}

- Anonymous September 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class MyHashMap {
      // for better re-sizing is taken as 2^4
      private static final int SIZE = 16;

      private Entry table[] = new Entry[SIZE];

      /**
       * To store the Map data in key and value pair.
       * Used linked list approach to avoid the collisions
       */
      class Entry {
            final String key;
            String value;
            Entry next;

            Entry(String k, String v) {
                  key = k;
                  value = v;
            }

            public String getValue() {
                  return value;
            }

            public void setValue(String value) {
                  this.value = value;
            }

            public String getKey() {
                  return key;
            }
      }

      /**
       * Returns the entry mapped to key in the HashMap.
       */
      public Entry get(String k) {
            int hash = k.hashCode() % SIZE;
            Entry e = table[hash];

            // Bucket is identified by hashCode and traversed the bucket
            // till element is not found.
            while(e != null) {
                  if(e.key.equals(k)) {
                        return e;
                  }
                  e = e.next;
            }
            return null;
      }

      /**
       * If the map previously contained a mapping for the key, the old
       * value is replaced.
       */
      public void put(String k, String v) {
            int hash = k.hashCode() % SIZE;
            Entry e = table[hash];

            if(e != null) {
                  // If we will insert duplicate key-value pair,
                  // Old value will be replaced by new one.
                  if(e.key.equals(k)) {
                        e.value = v;
                  } else {
                        // Collision: insert new element at the end of list
                        // in the same bucket
                        while(e.next != null) {
                              e = e.next;
                        }
                        Entry entryInOldBucket = new Entry(k, v);
                        e.next = entryInOldBucket;
                  }
            } else {
                  // create new bucket for new element in the map.
                  Entry entryInNewBucket = new Entry(k, v);
                  table[hash] = entryInNewBucket;
            }
      }

      public static void main(String[] args) {
            MyHashMap myHashMap = new MyHashMap();

            myHashMap.put("Awadh", "SSE");
            myHashMap.put("Rahul", "SSE");
            myHashMap.put("Sattu", "SE");
            myHashMap.put("Gaurav", "SE");

            Entry e = myHashMap.get("Awadh");
            System.out.println(""+e.getValue());
      }
}

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

public class MyHashMap {
// for better re-sizing is taken as 2^4
private static final int SIZE = 16;

private Entry table[] = new Entry[SIZE];

/**
* To store the Map data in key and value pair.
* Used linked list approach to avoid the collisions
*/
class Entry {
final String key;
String value;
Entry next;

Entry(String k, String v) {
key = k;
value = v;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getKey() {
return key;
}
}

/**
* Returns the entry mapped to key in the HashMap.
*/
public Entry get(String k) {
int hash = k.hashCode() % SIZE;
Entry e = table[hash];

// Bucket is identified by hashCode and traversed the bucket
// till element is not found.
while(e != null) {
if(e.key.equals(k)) {
return e;
}
e = e.next;
}
return null;
}

/**
* If the map previously contained a mapping for the key, the old
* value is replaced.
*/
public void put(String k, String v) {
int hash = k.hashCode() % SIZE;
Entry e = table[hash];

if(e != null) {
// If we will insert duplicate key-value pair,
// Old value will be replaced by new one.
if(e.key.equals(k)) {
e.value = v;
} else {
// Collision: insert new element at the end of list
// in the same bucket
while(e.next != null) {
e = e.next;
}
Entry entryInOldBucket = new Entry(k, v);
e.next = entryInOldBucket;
}
} else {
// create new bucket for new element in the map.
Entry entryInNewBucket = new Entry(k, v);
table[hash] = entryInNewBucket;
}
}

public static void main(String[] args) {
MyHashMap myHashMap = new MyHashMap();

myHashMap.put("Awadh", "SSE");
myHashMap.put("Rahul", "SSE");
myHashMap.put("Sattu", "SE");
myHashMap.put("Gaurav", "SE");

Entry e = myHashMap.get("Awadh");
System.out.println(""+e.getValue());
}
}

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

public class MyHashMap {
      // for better re-sizing is taken as 2^4
      private static final int SIZE = 16;

      private Entry table[] = new Entry[SIZE];

      /**
       * To store the Map data in key and value pair.
       * Used linked list approach to avoid the collisions
       */
      class Entry {
            final String key;
            String value;
            Entry next;

            Entry(String k, String v) {
                  key = k;
                  value = v;
            }

            public String getValue() {
                  return value;
            }

            public void setValue(String value) {
                  this.value = value;
            }

            public String getKey() {
                  return key;
            }
      }

      /**
       * Returns the entry mapped to key in the HashMap.
       */
      public Entry get(String k) {
            int hash = k.hashCode() % SIZE;
            Entry e = table[hash];

            // Bucket is identified by hashCode and traversed the bucket
            // till element is not found.
            while(e != null) {
                  if(e.key.equals(k)) {
                        return e;
                  }
                  e = e.next;
            }
            return null;
      }

      /**
       * If the map previously contained a mapping for the key, the old
       * value is replaced.
       */
      public void put(String k, String v) {
            int hash = k.hashCode() % SIZE;
            Entry e = table[hash];

            if(e != null) {
                  // If we will insert duplicate key-value pair,
                  // Old value will be replaced by new one.
                  if(e.key.equals(k)) {
                        e.value = v;
                  } else {
                        // Collision: insert new element at the end of list
                        // in the same bucket
                        while(e.next != null) {
                              e = e.next;
                        }
                        Entry entryInOldBucket = new Entry(k, v);
                        e.next = entryInOldBucket;
                  }
            } else {
                  // create new bucket for new element in the map.
                  Entry entryInNewBucket = new Entry(k, v);
                  table[hash] = entryInNewBucket;
            }
      }

      public static void main(String[] args) {
            MyHashMap myHashMap = new MyHashMap();

            myHashMap.put("Awadh", "SSE");
            myHashMap.put("Rahul", "SSE");
            myHashMap.put("Sattu", "SE");
            myHashMap.put("Gaurav", "SE");

            Entry e = myHashMap.get("Awadh");
            System.out.println(""+e.getValue());
      }

}

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

I think this is the most simple implementation and easy to understand. Thanks :) :)

- Naveen March 15, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is very helpful! Thanks.

- Anonymous September 01, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks for giving such a good and simple example .Given below piece of code
is only checking only first value in list and then appending value at end.here you are checking if other value in list may have duplicate key. Please have a look

if(e != null) {
                  // If we will insert duplicate key-value pair,
                  // Old value will be replaced by new one.
                  if(e.key.equals(k)) {
                        e.value = v;
                  } else {
                        // Collision: insert new element at the end of list
                        // in the same bucket
                        while(e.next != null) {
                              e = e.next;
                        }
                        Entry entryInOldBucket = new Entry(k, v);
                        e.next = entryInOldBucket;
                  }
            }
 
Please Correct if I am wrong
code snippet will be

while(e!=null){
if(e.key.equals(key)){
e.value=value;
break;
}else{
if(e.next!=null){
e=e.next;
}else{
Entry entryInOldBucket = new Entry(k, v);
 e.next = entryInOldBucket;
break;
}

}
}

- Anonymous September 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks for giving such a good and simple example .Given below piece of code  
is only checking only first value in list and then appending value at end.here you are checking if other value in list may have duplicate key. Please have a look  

if(e != null) {
                  // If we will insert duplicate key-value pair,
                  // Old value will be replaced by new one.
                  if(e.key.equals(k)) {
                        e.value = v;
                  } else {
                        // Collision: insert new element at the end of list
                        // in the same bucket
                        while(e.next != null) {
                              e = e.next;
                        }
                        Entry entryInOldBucket = new Entry(k, v);
                        e.next = entryInOldBucket;
                  }
            }
 
Please Correct if I am wrong
code snippet will be

while(e!=null){
if(e.key.equals(key)){
e.value=value;
break;
}else{
if(e.next!=null){
e=e.next;
}else{
Entry entryInOldBucket = new Entry(k, v);
 e.next = entryInOldBucket;
break;
}

}
}

- Anonymous September 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks for giving such a good and simple example .Given below piece of code
is only checking only first value in list and then appending value at end.here you are checking if other value in list may have duplicate key. Please have a look

if(e != null) {
// If we will insert duplicate key-value pair,
// Old value will be replaced by new one.
if(e.key.equals(k)) {
e.value = v;
} else {
// Collision: insert new element at the end of list
// in the same bucket
while(e.next != null) {
e = e.next;
}
Entry entryInOldBucket = new Entry(k, v);
e.next = entryInOldBucket;
}
}

Please Correct if I am wrong
code snippet will be

while(e!=null){
if(e.key.equals(key)){
e.value=value;
break;
}else{
if(e.next!=null){
e=e.next;
}else{
Entry entryInOldBucket = new Entry(k, v);
e.next = entryInOldBucket;
break;
}

}
}

- Anonymous September 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package shortProjects;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ImplementHashMap {

	public static void main(String[] args) {
		MyHashMap<Integer, String> myMap = new MyHashMap<Integer, String>();
		myMap.put(1, "Jade");
		myMap.put(5, "Smith");
		System.out.println(myMap.get(1));
		System.out.println(myMap.get(5));
		System.out.println(myMap.get(2));
		System.out.println(myMap.keySet());
		System.out.println(myMap.values());
		System.out.println(myMap.toString());
	}

}

class MyEntry<K,V> {
	private K key;
	private V value;
	
	public MyEntry(K key, V value){
		this.key = key;
		this.value = value;
	}
	public K getKey() {
		return key;
	}
	public void setKey(K key) {
		this.key = key;
	}
	public V getValue() {
		return value;
	}
	public void setValue(V value) {
		this.value = value;
	}
}

class MyHashMap<K,V> {
	
	List<MyEntry<K,V>> list;
	private int size;
	
	public <K,V> MyHashMap() {
		size = 16;
		list = new ArrayList(size);
		for(int i=0; i<size; i++) {
			list.add(null);
		}
	}

	public int size() {
		return list.size();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public boolean containsKey(Object key) {
		int index = getKey(key);
		if(list.get(index) != null)
			return true;
		return false;
	}

	public boolean containsValue(Object value) {
		boolean result = false;
		for(int i=0; i<size; i++) {
			if(list.get(i) != null && list.get(i).getValue() == value) {
				result = true;
			}
		}
		return result;
	}

	public V get(Object key) {
		int index = getKey(key);
		MyEntry<K, V> entry = list.get(index);
		return entry != null ? list.get(index).getValue() : null;
	}

	public void put(K key, V value) {
		int index = getKey(key);
		MyEntry<K,V> entry = new MyEntry(key, value);
		list.add(index, entry);
	}

	public void remove(Object key) {
		int index = getKey(key);
		list.add(index, null);
	}
	
	public int getKey(Object key) {
		return Math.abs(key.hashCode() % size);
	}

	public void putAll(Map<? extends K, ? extends V> m) {
		// TODO Auto-generated method stub
		
	}

	public void clear() {
		for(int i=0; i<size; i++) {
			list.add(null);
		}
	}

	public Set<K> keySet() {
		Set<K> result = list.stream().filter(entry -> entry!=null).map(entry -> entry.getKey()).collect(Collectors.toSet());
		return result;
	}

	public Collection<V> values() {
		Set<V> result = list.stream().filter(entry -> entry!=null).map(entry -> entry.getValue()).collect(Collectors.toSet());
		return result;
	}

	public Set<MyEntry<K, V>> entrySet() {
		Set<MyEntry<K, V>> result = list.stream().filter(entry -> entry != null).collect(Collectors.toSet());
		return result;
	}
	
	public String toString() {
		String result = list.stream()
							.filter(entry -> entry!=null)
							.map(entry -> "{" + entry.getKey() + "=" + entry.getValue() + "}")
							.collect(Collectors.joining(","));
		return result;
	}
	
	public void printAll() {
		System.out.println(list);
	}
	
}

- Zesta114 April 04, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

HashMap with generics.

package shortProjects;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ImplementHashMap {

	public static void main(String[] args) {
		MyHashMap<Integer, String> myMap = new MyHashMap<Integer, String>();
		myMap.put(1, "Jade");
		myMap.put(5, "Smith");
		System.out.println(myMap.get(1));
		System.out.println(myMap.get(5));
		System.out.println(myMap.get(2));
		System.out.println(myMap.keySet());
		System.out.println(myMap.values());
		System.out.println(myMap.toString());
	}

}

class MyEntry<K,V> {
	private K key;
	private V value;
	
	public MyEntry(K key, V value){
		this.key = key;
		this.value = value;
	}
	public K getKey() {
		return key;
	}
	public void setKey(K key) {
		this.key = key;
	}
	public V getValue() {
		return value;
	}
	public void setValue(V value) {
		this.value = value;
	}
}

class MyHashMap<K,V> {
	
	List<MyEntry<K,V>> list;
	private int size;
	
	public <K,V> MyHashMap() {
		size = 16;
		list = new ArrayList(size);
		for(int i=0; i<size; i++) {
			list.add(null);
		}
	}

	public int size() {
		return list.size();
	}

	public boolean isEmpty() {
		return list.isEmpty();
	}

	public boolean containsKey(Object key) {
		int index = getKey(key);
		if(list.get(index) != null)
			return true;
		return false;
	}

	public boolean containsValue(Object value) {
		boolean result = false;
		for(int i=0; i<size; i++) {
			if(list.get(i) != null && list.get(i).getValue() == value) {
				result = true;
			}
		}
		return result;
	}

	public V get(Object key) {
		int index = getKey(key);
		MyEntry<K, V> entry = list.get(index);
		return entry != null ? list.get(index).getValue() : null;
	}

	public void put(K key, V value) {
		int index = getKey(key);
		MyEntry<K,V> entry = new MyEntry(key, value);
		list.add(index, entry);
	}

	public void remove(Object key) {
		int index = getKey(key);
		list.add(index, null);
	}
	
	public int getKey(Object key) {
		return Math.abs(key.hashCode() % size);
	}

	public void putAll(Map<? extends K, ? extends V> m) {
		// TODO Auto-generated method stub
		
	}

	public void clear() {
		for(int i=0; i<size; i++) {
			list.add(null);
		}
	}

	public Set<K> keySet() {
		Set<K> result = list.stream().filter(entry -> entry!=null).map(entry -> entry.getKey()).collect(Collectors.toSet());
		return result;
	}

	public Collection<V> values() {
		Set<V> result = list.stream().filter(entry -> entry!=null).map(entry -> entry.getValue()).collect(Collectors.toSet());
		return result;
	}

	public Set<MyEntry<K, V>> entrySet() {
		Set<MyEntry<K, V>> result = list.stream().filter(entry -> entry != null).collect(Collectors.toSet());
		return result;
	}
	
	public String toString() {
		String result = list.stream()
							.filter(entry -> entry!=null)
							.map(entry -> "{" + entry.getKey() + "=" + entry.getValue() + "}")
							.collect(Collectors.joining(","));
		return result;
	}
	
	public void printAll() {
		System.out.println(list);
	}
	
}

- Zesta114 April 04, 2019 | 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