注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号
x
本帖最后由 匿名 于 2022-7-10 16:53 编辑
猎头推的Sr. Backend,6月底电面,面试官是1个Asian女生和1个国人男生。
1. 只是试水很简单,implement a math power method- // a = 2, b = 4, a^b = 16
- // a = 2, b = -4, a^b = 1/16
- public double pow(double a, int b) {
- if(a == 0) return 0;
- if(b == 0) return 1;
- double res = 1;
- for(int i = 0; i < Math.abs(b); i++)
- res *= a;
- if(b >= 0)
- return res;
- else
- return 1 / res;
- }
复制代码 2. LL的经典题RetainBestCache,奉上solution,应该不能运行但面试官说okay。可以参照此贴 - // cache: [(cat, 3),(snake, 1), (spider, 10)]
- // cpacity = 3
- // rbc.get(snake)
- // db.get(snake) -> 1
- // return 1
- public class RetainBestCache<K, V extends Rankable> {
- int cap;
- HashMap<K, V> cache;
- PriorityQueue<Entry<K, V>> pq;
- DataSource<K, V> ds;
- public RetainBestCache(DataSource<K, V> ds, int capacity) {
- // implementation
- cache = new HashMap<>();
- pq = new PriorityQueue<>(new Comparator<Entry<K, V>>(){
- @Override
- public int compare(Entry<K, V> e1, Entry<K, V> e2) {
- return e1.getRank() - e2.getRank();
- }
- });
- this.ds = ds;
- cap = capacity;
- }
- public V get(K key) {
- // Implementation here
- if(cache.containsKey(key))
- return cache.get(key);
-
- V val = ds.get(key);
- if(cache.size() < cap) {
- cache.put(key, val);
- pq.offer(new Entry(key, val));
- } else {
- Entry<K, V> top = pq.poll();
- cache.remove(top.getKey());
-
- cache.put(key, val);
- pq.offer(new Entry(key, val));
- }
- return val;
- }
- }
- /*
- * For reference, here are the Rankable and DataSource interfaces.
- * You do not need to implement them, and should not make assumptions
- * about their implementations.
- */
-
- public interface Rankable {
- /**
- * Returns the Rank of this object, using some algorithm and potentially
- * the internal state of the Rankable.
- */
- long getRank();
- }
-
- public interface DataSource<K, V extends Rankable> {
- V get (K key);
- }
复制代码 整个面试整体感觉还不错,但没后续了,发邮件给res = power(x, n / 2);
if(n % 2 == 0)
return res * res;
else
return res * res * x;
}
} |