Goldman Sachs Interview Question
Developer Program EngineersCountry: India
Interview Type: Written Test
package pank.com.org;
public class MyOwnHashMap {
private final static int size =16;
private Entry table[]= new Entry[size];
static class Entry
{ final String key;
String value;
Entry next;
public Entry(String K, String V) { key=K;
value=V; }
public void setValue(String value){
this.value= value; }
public String getKey() {
return this.key;
}
public String getValue()
{ return this.value;
} }
public Entry get(String key)
{ int hash = key.hashCode()%size;
Entry e= table[hash];
while(e!=null)
{
if(e.key.equals(key))
{ return e;
} e = e.next;
}return null;
}
public void put(String key, String value)
{ int hash = key.hashCode() % size;
System.out.println(key.hashCode());
Entry e = table[hash];
if(e!=null){if(e.key.equals(key)){
e.value=value;}else
{while(e.next!=null){e=e.next;
}
Entry oldEntry = new Entry(key,value);
e.next= oldEntry;}
}
else{Entry newEntry = new Entry(key,value);
table[hash]=newEntry;}
} public static void main (String args[])
{
MyOwnHashMap map= new MyOwnHashMap();
map.put("Chandu", "SMTS");
map.put("Shan", "SMTS");
map.put("Chanda", "SSE");
map.put("Niranjan", "SMTS1");
map.put("Chandu", "SSE");
Entry e= map.get("Chandu");
System.out.println( "key>>>>" +e.getKey());
System.out.println("value >>>>"+ e.getValue() );
System.out.println( "class>>>>" + e.getClass());
System.out.println("Hascode"+e.hashCode());
Entry e1= map.get("Niranjan");
System.out.println( "key>>>>" +e1.getKey());
System.out.println("value >>>>"+ e1.getValue() );
System.out.println( "class>>>>" + e1.getClass());
System.out.println("Hascode"+e1.hashCode());
}
}
package pank.com.org;
public class MyOwnHashMap {
private final static int size =16;
private Entry table[]= new Entry[size];
static class Entry
{ final String key;
String value;
Entry next;
public Entry(String K, String V) { key=K;
value=V; }
public void setValue(String value){
this.value= value; }
public String getKey() {
return this.key;
}
public String getValue()
{ return this.value;
} }
public Entry get(String key)
{ int hash = key.hashCode()%size;
Entry e= table[hash];
while(e!=null)
{
if(e.key.equals(key))
{ return e;
} e = e.next;
}return null;
}
public void put(String key, String value)
{ int hash = key.hashCode() % size;
System.out.println(key.hashCode());
Entry e = table[hash];
if(e!=null){if(e.key.equals(key)){
e.value=value;}else
{while(e.next!=null){e=e.next;
}
Entry oldEntry = new Entry(key,value);
e.next= oldEntry;}
}
else{Entry newEntry = new Entry(key,value);
table[hash]=newEntry;}
} public static void main (String args[])
{
MyOwnHashMap map= new MyOwnHashMap();
map.put("Chandu", "SMTS");
map.put("Shan", "SMTS");
map.put("Chanda", "SSE");
map.put("Niranjan", "SMTS1");
map.put("Chandu", "SSE");
Entry e= map.get("Chandu");
System.out.println( "key>>>>" +e.getKey());
System.out.println("value >>>>"+ e.getValue() );
System.out.println( "class>>>>" + e.getClass());
System.out.println("Hascode"+e.hashCode());
Entry e1= map.get("Niranjan");
System.out.println( "key>>>>" +e1.getKey());
System.out.println("value >>>>"+ e1.getValue() );
System.out.println( "class>>>>" + e1.getClass());
System.out.println("Hascode"+e1.hashCode());
}
}
package pank.com.org;
- own hashMap August 27, 2015public class MyOwnHashMap {
private final static int size =16;
private Entry table[]= new Entry[size];
static class Entry
{
final String key;
String value;
Entry next;
public Entry(String K, String V) {
key=K;
value=V;
}
public void setValue(String value)
{
this.value= value;
}
public String getKey()
{
return this.key;
}
public String getValue()
{
return this.value;
}
}
public Entry get(String key)
{
int hash = key.hashCode()%size;
Entry e= table[hash];
while(e!=null)
{
if(e.key.equals(key))
{
return e;
}
e = e.next;
}
return null;
}
public void put(String key, String value)
{
int hash = key.hashCode() % size;
System.out.println(key.hashCode());
Entry e = table[hash];
if(e!=null){
if(e.key.equals(key))
{
e.value=value;
}
else
{
while(e.next!=null)
{
e=e.next;
}
Entry oldEntry = new Entry(key,value);
e.next= oldEntry;
}
}
else
{
Entry newEntry = new Entry(key,value);
table[hash]=newEntry;
}
}
public static void main (String args[])
{
MyOwnHashMap map= new MyOwnHashMap();
map.put("Chandu", "SMTS");
map.put("Shan", "SMTS");
map.put("Chanda", "SSE");
map.put("Niranjan", "SMTS1");
map.put("Chandu", "SSE");
Entry e= map.get("Chandu");
System.out.println( "key>>>>" +e.getKey());
System.out.println("value >>>>"+ e.getValue() );
System.out.println( "class>>>>" + e.getClass());
System.out.println("Hascode"+e.hashCode());
Entry e1= map.get("Niranjan");
System.out.println( "key>>>>" +e1.getKey());
System.out.println("value >>>>"+ e1.getValue() );
System.out.println( "class>>>>" + e1.getClass());
System.out.println("Hascode"+e1.hashCode());
}
}