Yahoo Interview Question for Software Development Managers


Country: United States
Interview Type: Phone Interview




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

public class MyHashMap<K,T> {
	static class Pair<K,T>{
		K key;
		T value;
		Pair(K k,T v){
			key = k;
			value = v;
		}
	}
	int size=10;
	LinkedList buckets[]=new LinkedList[size];
	int hash(K key){
		return key.hashCode()%size;
	}
	void put(K key,T value){
		int index=hash(key);
		if(buckets[index]==null){
			buckets[index]=new LinkedList();
		}
		for(Object p:buckets[index]){
			Pair<K,T> pair=(Pair<K,T>)p;
			if(pair.key.equals(key)){
				pair.value=value;
				return;
			}
		}
		buckets[index].add(new Pair(key,value));
	}
	T get(K key){
		int index=hash(key);
		if(buckets[index]==null) return null;
		for(Object p:buckets[index]){
			Pair<K,T> pair=(Pair<K,T>)p;
			if(pair.key.equals(key))
				return pair.value;
		}
		return null;
	}
	

}

- Charlie January 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Thanks for solution Charlie. Here is code with test data:

public class MyHashMap<K, T> {

	public static void main(String[] args) {
		MyHashMap<String, String> map = new MyHashMap<String, String>();
		map.put("CA", "California");
		map.put("TX", "Texas");
		map.put("TX", "Texas1");
		map.put("TX", "Texas2");
		map.put("GA", "Georgia");
		String s = map.get("CA");
		System.out.println(map.get("CA"));
		System.out.println(map.get("TX"));
		System.out.println(map.get("GA"));

	}

	static class Pair<K, T> {
		K key;
		T value;

		Pair(K k, T v) {
			key = k;
			value = v;
		}
	}

	int size = 10;
	LinkedList buckets[] = new LinkedList[size];

	int hash(K key) {
		return key.hashCode() % size;
	}

	void put(K key, T value) {
		int index = hash(key);
		if (buckets[index] == null) {
			buckets[index] = new LinkedList();
		}
		for (Object p : buckets[index]) {
			Pair<K, T> pair = (Pair<K, T>) p;
			if (pair.key.equals(key)) {
				pair.value = value;
				return;
			}
		}
		buckets[index].add(new Pair(key, value));
	}

	T get(K key) {
		int index = hash(key);
		if (buckets[index] == null)
			return null;
		for (Object p : buckets[index]) {
			Pair<K, T> pair = (Pair<K, T>) p;
			if (pair.key.equals(key))
				return pair.value;
		}
		return null;
	}

}

- pawanKjajara February 14, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Map

- Manisha February 16, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.


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