Uber Interview Question for Software Developers


Country: United States
Interview Type: In-Person




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


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