注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
本帖最后由 匿名 于 2025-6-11 20:28 编辑
前段时间面 databricks 总结了一下他们的面试题,截止到去年10月好像。我面试的时候都是下面的原题
求米!!🥺 祝大家都能过
Algorithm Problems
- Delete Interval
- Delete index from interval
- Max Area of Island (dfs, O(M*N))
- LC695 - Max Area of Island
- SnapshotSet
- SnapshotSet with versioned iterator (结合 LC1146 思路)
实现一个set,需要一个支持当前状态的iterator,无论是put还是remove,不影响iterator之后的遍历
- KV Store
- LC 981,设计一个支持高效 get/put 和 query 的 KV store
技术点:滑动窗口,TreeMap 或双端队列处理时间戳。yArray([10, 20, 30, 40, 50])
arr.map(lambda x:x*2).indexOf(40) ----> 1
arr.map(lambda x:x2).map(lambda x:x3).indexOf(240) ----> 3 注意这里重新开了一个chain,上一行的map就不计算在内了 - Each map() must return a new LazyArray, because chains are independent
- [backcolor=rgb(247, 247, 247)]import java.util.*;[/backcolor][list]
- [list]
- [*][code]import java.util.function.Function;
- public class LazyArray {
- private List<Integer> base;
- private List<Function<Integer, Integer>> operations;
- public LazyArray(List<Integer> base) {
- this.base = base;
- this.operations = new ArrayList<>();
- }
- private LazyArray(List<Integer> base, List<Function<Integer, Integer>> ops) {
- this.base = base;
- this.operations = ops;
- }
- public LazyArray map(Function<Integer, Integer> func) {
- List<Function<Integer, Integer>> newOps = new ArrayList<>(this.operations);
- newOps.add(func);
- return new LazyArray(this.base, newOps);
- }
- public int indexOf(int target) {
- for (int i = 0; i < base.size(); i++) {
- int val = base.get(i);
- for (Function<Integer, Integer> op : operations) {
- val = op.apply(val);
- }
- if (val == target) {
- return i;
- }
- }
- return -1;
- }
- }
复制代码
2. Circuit Breaker- import java.time.Duration;
- import java.time.Instant;
- import java.util.concurrent.atomic.*;
- import java.util.function.Supplier;
-
- public class CircuitBreaker<T> {
-
- private enum State {
- CLOSED, OPEN, HALF_OPEN
- }
-
- private final AtomicReference<State> state = new AtomicReference<>(State.CLOSED);
- private final AtomicInteger failureCount = new AtomicInteger(0);
- private final AtomicReference<Instant> lastFailureTime = new AtomicReference<>(null);
-
- private final int failureThreshold;
- private final Duration recoveryTimeout;
-
- public CircuitBreaker(int failureThreshold, Duration recoveryTimeout) {
- this.failureThreshold = failureThreshold;
- this.recoveryTimeout = recoveryTimeout;
- }
-
- // or we can use synchronized and don't need to use atomic
- public T call(Supplier<T> supplier) throws Exception {
- Instant now = Instant.now();
- State currentState = state.get();
-
- if (currentState == State.OPEN) {
- Instant lastFailure = lastFailureTime.get();
- if (lastFailure != null &&
- Duration.between(lastFailure, now).compareTo(recoveryTimeout) > 0) {
- // Try to transition from OPEN → HALF_OPEN
- if (!state.compareAndSet(State.OPEN, State.HALF_OPEN)) {
- throw new RuntimeException("Circuit is OPEN. Request blocked.");
- }
- } else {
- throw new RuntimeException("Circuit is OPEN. Request blocked.");
- }
- }
-
- try {
- T result = supplier.get();
- onSuccess();
- return result;
- } catch (Exception e) {
- onFailure();
- throw e;
- }
- }
-
- private void onSuccess() {
- failureCount.set(0);
- if (state.get() == State.HALF_OPEN) {
- state.compareAndSet(State.HALF_OPEN, State.CLOSED);
- }
- }
-
- private void onFailure() {
- int failures = failureCount.incrementAndGet();
- lastFailureTime.set(Instant.now());
-
- if (failures >= failureThreshold) {
- state.set(State.OPEN);
- } else if (state.get() == State.HALF_OPEN) {
- state.compareAndSet(State.HALF_OPEN, State.OPEN);
- }
- }
-
- public String getState() {
- return state.get().name();
- }
- }
复制代码 3. Encoding & Decoding
- Encoding & Decoding implementation
- import java.util.*;
-
- interface Run {
- String encode();
- }
-
- class RLERun implements Run {
- int value;
- int count;
-
- RLERun(int value, int count) {
- this.value = value;
- this.count = count;
- }
-
- @Override
- public String encode() {
- return "RLE[" + value + ", " + count + "]";
- }
- }
-
- class BPRun implements Run {
- List<Integer> values;
-
- BPRun(List<Integer> values) {
- this.values = values;
- }
-
- @Override
- public String encode() {
- return "BP" + values.toString();
- }
- }
-
- class Encoder {
- public List<Run> encode(int[] input) {
- List<Run> result = new ArrayList<>();
- int i = 0;
- while (i < input.length) {
- int j = i;
- while (j < input.length && input[j] == input[i]) {
- j++;
- }
- int count = j - i;
- if (count >= 8) {
- result.add(new RLERun(input[i], count));
- i = j;
- } else {
- int bpEnd = Math.min(input.length, i + 8);
- List<Integer> bpValues = new ArrayList<>();
- for (int k = i; k < bpEnd; k++) {
- bpValues.add(input[k]);
- }
- result.add(new BPRun(bpValues));
- i += bpValues.size();
- }
- }
- return result;
- }
- }
-
- class Decoder {
- public List<Integer> decode(List<Run> runs) {
- List<Integer> output = new ArrayList<>();
- for (Run run : runs) {
- if (run instanceof RLERun) {
- RLERun rle = (RLERun) run;
- for (int i = 0; i < rle.count; i++) {
- output.add(rle.value);
- }
- } else if (run instanceof BPRun) {
- output.addAll(((BPRun) run).values);
- }
- }
- return output;
- }
- }
-
- class Main {
- public static void main(String[] args) {
- Encoder encoder = new Encoder();
- Decoder decoder = new Decoder();
-
- int[] input = {1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5};
- List<Run> encoded = encoder.encode(input);
- for (Run run : encoded) {
- System.out.println(run.encode());
- }
-
- List<Integer> decoded = decoder.decode(encoded);
- System.out.println("Decoded: " + decoded);
- }
- }
复制代码 |