Adyen OA 面试真题解析:Fraudster Cache(最近访问淘汰)

19次阅读
没有评论

Given a maximum capacity, return an implementation of a fraudster cache.

Adyen's fraud team has identified that fraudsters often try multiple card numbers in short periods of time. Therefore, it's useful to keep a small list of shoppers in memory — the ones that most recently attempted fraud or were marked as fraudulent. Given that fraud detection happens before payment authorizations, the cache is expected to perform very well.

Given a maximum capacity, return an implementation of a fraudster cache.

Sample test case:

On a fraudster cache with a capacity of 2, we do the following 6 operations:
1) Mark the shopper with attributes (shopper1, 205.13.50.50, fraud@gmail.com) as fraudulent.
   The cache now contains shopper1.
2) Mark the shopper with attributes (shopper2, 201.16.13.55, fraud@yahoo.com) as fraudulent.
   The cache now contains shopper1 and shopper2.
3) Shopper shopper1 attempts to pay, we get them from the cache and return their details.
   The fraud attempt for shopper1 is reflected in the cache.
4) Mark the shopper with attributes (shopper3, 202.56.0.17, fraud@example.com) as fraudulent.
   shopper3 should be added to the cache, but we're at capacity, so we need to remove 1 of the 2 shoppers from the cache. We remove shopper2 — even though shopper1 was marked as fraud first, they recently attempted fraud.
   The cache now contains shopper1 and shopper3.
5) Get shopper shopper1
   Return the shopper attributes (shopper1, 205.13.50.50, fraud@gmail.com)
6) Get shopper shopper2
   This shopper was removed from the cache to make room for shopper3, we no longer have their details in memory so we return null.

这题本质上是一个带容量限制的最近使用缓存:每次 <code>markFraud</code> 或 <code>getFraudster</code> 都会让该 shopper 变成“最新被访问”的对象,当缓存满了以后,就淘汰最久未被访问的那一位。最佳做法通常是用 <code>LinkedHashMap</code>(开启 access-order)或“哈希表 + 双向链表”来同时保证按 ID 快速查找和 O(1) 级别的更新、删除、淘汰。样例里 shopper1 在被读取后变得更新,因此新增 shopper3 时被移除的是 shopper2,而不是更早标记的 shopper1。

正文完
 0