Google Interview Question for Software Engineer / Developers


Country: United States




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

Implement two-level of hashing, two hash functions

h1( typename ) returns a key k1 in table T1 which contains links to a separate table T2(one T2 per entry of T1)
h2(k1, object) returns a key k2 (links to entry within T2)

To hash h2( object.class, object ), first hash gives a key to location of object in an entire arrangement of two hash tables.

- confused_banda November 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why Can't we create Hash map with key and value both of object type and overwrite the methods for specific type

- Anonymous November 23, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Please find the Implementation

public class CustomHashMap {

    private LinkedHashEntry[] hashTable;
    private static final int HASH_LENGTH = 100;
    public CustomHashMap(){
        hashTable = new LinkedHashEntry[HASH_LENGTH];
    }

    public void put(Object key,Object value){
        LinkedHashEntry node = getNode(key);

        if(node == null){
            hashTable[getHashCode(key)]=new LinkedHashEntry(key, value);
            return;
        }

        while(node.hasNext() && !(node.getKey().equals(key)))
              node=node.next;

        //updation
        if(key.equals(node.getKey()))
            node.value=value;
        System.out.println(" Node "+node.getKey()+"  "+ node.value+ "  "+node.next);
        //addition
        LinkedHashEntry newEntry = new LinkedHashEntry(key, value);
                        node.next=newEntry;

        System.out.println(" Node "+node.getKey()+"  "+ node.value+ "  "+node.next);
        System.out.println(" newEntry "+newEntry.getKey()+"  "+ newEntry.value+ "  "+newEntry.next);
        System.out.println(newEntry);

    }

    public Object get(Object key){

        if(key == null)
            return null;

        LinkedHashEntry node = getNode(key);
          if(node == null)
             return null;

         while(node.hasNext() && !key.equals(node.getKey()))
               node=node.next;

        if(key.equals(node.getKey()))
            return node.value;

        return null;
    }

    public Boolean remove(Object key){

        if(key == null)
            return false;

        LinkedHashEntry node = getNode(key);
        LinkedHashEntry previousNode=node;

        while(node.hasNext() && !key.equals(node.getKey())){
              previousNode=node;
              node=node.next;
        }

              if(key.equals(node.getKey())){
                  previousNode.next=node.next;
                  node.next=null;
                  return true;
              }


        return false;
    }

    private LinkedHashEntry getNode(Object key) {
        System.out.println("getHashCode(key)   "+ getHashCode(key));
        return hashTable[getHashCode(key)];
    }

    private int getHashCode(Object key) {
        return (key.hashCode() %  HASH_LENGTH);
    }

    private class LinkedHashEntry{

        private Object key;
        private Object value;
        private LinkedHashEntry next;
        LinkedHashEntry(Object key,Object value){

             this.key=key;
             this.value=value;
             this.next=null;
        }

        Object getKey(){
            return key;
        }

        Boolean hasNext(){
            return (next != null);
        }


    }


    public static void main(String ... s){
        CustomHashMap customHashMap = new CustomHashMap();

        customHashMap.put("hi","hi");
        customHashMap.put(29,"hi bye");

    }
}

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

What the problem? Implement Map<?,?> interface

- glebstepanov1992 November 15, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Read the question properly. I want the hash function implement that can handle any type of data(say String, Integer, Object etc) , hash it and the result should be the index of the hashArray.

- Read Properly November 15, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

use memory address

- EK MACHCHAR November 17, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class Hash <Key, Value>{

Key ob1;
Value ob2;

Hash(Key ob1, Value ob2)
{
this.ob1 = ob1;
this.ob2 = ob2;
}

Key getOb1()
{
return ob1;
}

Value getOb2()
{
return ob2;
}
}

- Hemant Malik July 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class Hash <Key, Value>{

	Key ob1;
	Value ob2;
	
	Hash(Key ob1, Value ob2)
	{
		this.ob1 = ob1;
		this.ob2 = ob2;
	}
	
	Key getOb1()
	{
		return ob1;
	}
	
	Value getOb2()
	{
		return ob2;
	}
}

- Hemant Malik July 22, 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