WePay 面试
Wepay的面试很容易拿, 扔个简历过去,很快就换个oa回来. oa的题也很固定2个选择题, 1个设计hashtable题. 但是这个hashtable题很奇葩, 因为oa就30分钟, 而且用的是hackerrank的环境, 根本没法复制粘贴,全靠手敲代码+测试, 要求是能支持put和get方法, 并且是有抽象参数, 并且还要能解决hash冲突. 然后我敲了下面的代码, 居然说oa没过..然后发网上, 问了过了的同学怎么写的…大家纷纷表示我写的很好- – move on 了
import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; public class myHashTable<K, V> { class Entry<K, V> { public Entry(K key, V value) { this.key = key; this.value = value; } K key; V value; } private int size = 1000; private ArrayList<LinkedList<Entry<K, V>>> ary = new ArrayList<>(size); public myHashTable(int size) { this.size = size; for (int i = 0; i < size; i++) { ary.add(i, null); } } public void put(K key, V value) { if (key == null) throw new NullPointerException("null of key is not allowed"); if (ary.get(key.hashCode() % size) == null) ary.set(key.hashCode() % size, new LinkedList<Entry<K, V>>()); ary.get(key.hashCode() % size).addFirst(new Entry<K, V>(key, value)); } public V get(K key) { if (key == null) throw new NullPointerException("null of key is not allowed"); if (ary.get(key.hashCode() % size) == null) return null; else { LinkedList<Entry<K, V>> res = ary.get(key.hashCode() % size); Iterator<Entry<K, V>> iterator = res.iterator(); while (iterator.hasNext()) { Entry<K, V> e = iterator.next(); if (e.key.equals(key)) return e.value; } return null; } } public static void main(String[] args) { myHashTable<String, String> m = new myHashTable<>(1000); m.put("abc", "eee"); m.put("abc", "bbb"); m.put("eee","ddd"); System.out.println(m.get("eee")); } }
Leave A Comment