← WeRide Interview InsightsSoftware Engineer

WeRide·Software Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
Jun 2026

Summary

Weride put me through a pretty deep Java concurrency session, the kind where you realize halfway through that you've been fuzzy on some of this stuff for years and just never had to confront it.

Questions Asked (7)

Q1

What are the different ways to create threads in Java, and how do you decide which approach to use?

Technical Trade-offsSystem Design
Author's notes

Ran through Thread subclassing, Runnable, and ExecutorService.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by listing the main ways to create threads in Java: extending Thread, implementing Runnable, using Callable with FutureTask, and leveraging ExecutorService. Then discuss the trade-offs of each approach, focusing on flexibility, return values, exception handling, and resource management. Conclude with a decision framework based on task requirements and system design considerations.

Pro tip: Emphasize that in modern Java, directly creating threads is discouraged; instead, use ExecutorService or the Fork/Join framework to manage thread pools efficiently. Mention that implementing Runnable or Callable is preferred over extending Thread to avoid limiting inheritance and to separate task from execution mechanism.

1. Enumerate thread creation methods

List the core ways: extending Thread class, implementing Runnable interface, using Callable with FutureTask, and using ExecutorService to manage threads. Briefly describe each.

2. Compare trade-offs

Discuss pros and cons: inheritance limitation with Thread, flexibility and separation of concerns with Runnable, return values and exception handling with Callable, and resource management and scalability with ExecutorService.

3. Consider system design implications

Explain how thread creation impacts system design: thread lifecycle overhead, resource contention, scalability, and the need for thread pools in high-concurrency scenarios.

4. Provide a decision framework

Outline criteria for choosing: use Runnable/Callable for simple tasks, ExecutorService for managing multiple tasks and controlling concurrency, and specialized frameworks (e.g., Fork/Join) for divide-and-conquer parallelism.

5. Summarize best practices

Conclude with modern best practices: prefer ExecutorService over manual thread creation, use Callable for tasks needing results, and avoid extending Thread unless necessary.

Key Points to Mention

  • Extending Thread vs implementing Runnable: Java allows only single inheritance, so implementing Runnable is more flexible.
  • Callable and Future: allow tasks to return results and throw checked exceptions, unlike Runnable.
  • ExecutorService and thread pools: manage thread lifecycle, reduce overhead, and improve resource utilization.
  • Thread creation overhead: creating new threads is expensive; pooling is essential for high-performance systems.
  • Concurrency utilities: java.util.concurrent provides higher-level abstractions like ExecutorService, ForkJoinPool, and CompletableFuture.
  • Decision factors: task duration, need for return values, exception handling, scalability, and resource constraints.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Explain the difference between synchronized, Lock, and ReentrantLock. When would you reach for one over another?

Technical Trade-offsSystem Design
Author's notes

This felt like a trap disguised as a warmup.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining each mechanism and highlighting their core differences in terms of syntax, flexibility, and performance. Then discuss trade-offs like fairness, interruptibility, and condition variables, and finally give concrete scenarios for when to use each. Emphasize that synchronized is simplest but Lock/ReentrantLock offer advanced control.

Pro tip: Mention that in modern JVMs, synchronized performance is highly optimized and often comparable to ReentrantLock, so the choice should be based on required features rather than raw speed. Also, note that ReentrantLock must be unlocked in a finally block to avoid deadlocks.

1. Define synchronized

Explain that synchronized is a built-in Java keyword providing intrinsic locking, with automatic lock acquisition and release, and is used for simple mutual exclusion.

2. Define Lock and ReentrantLock

Describe Lock as an interface offering explicit lock/unlock methods, and ReentrantLock as its common implementation that adds reentrancy, fairness, and advanced features like tryLock and lockInterruptibly.

3. Compare key features

Contrast them on dimensions: flexibility (explicit control vs. block-structured), fairness (optional vs. none), interruptibility, condition variables, and performance under contention.

4. Discuss trade-offs and use cases

Provide scenarios: use synchronized for simple, low-contention locking; use ReentrantLock when you need timed tryLock, interruptible locks, fair queuing, or multiple condition variables.

5. Conclude with best practices

Summarize that synchronized is safer and less error-prone, while ReentrantLock offers power at the cost of manual unlock; choose based on specific requirements.

Key Points to Mention

  • synchronized is a keyword with implicit lock management; Lock is an interface with explicit lock/unlock.
  • ReentrantLock supports fairness policy, tryLock with timeout, lockInterruptibly, and multiple Condition objects.
  • synchronized cannot be interrupted while waiting for a lock; ReentrantLock can via lockInterruptibly.
  • ReentrantLock must be unlocked in a finally block to prevent deadlocks; synchronized releases automatically.
  • Performance: synchronized is optimized in modern JVMs; ReentrantLock may be better under high contention with advanced features.
  • Use synchronized for simple cases; use ReentrantLock for advanced control, fairness, or non-block-structured locking.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

How does the Java memory model work, and what does volatile actually guarantee?

Technical Trade-offsAlgorithms & Data Structures
Author's notes

volatile is one of those things where you think you know it until someone asks you to be precise.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining the Java Memory Model (JMM) as an abstract specification that governs how threads interact through memory, then explain the happens-before relationship and how volatile establishes it. Finally, clarify what volatile guarantees (visibility and ordering) and what it does not (atomicity), using a concrete example like a flag variable.

Pro tip: Mention that volatile is often used in lock-free algorithms and that on x86 it's essentially a memory fence, but avoid claiming it's a performance silver bullet—show awareness of its limitations and when to use synchronized or java.util.concurrent atomics instead.

1. Define the JMM and its purpose

Explain that the JMM specifies how threads see memory writes and how reordering and caching can affect visibility. It defines the happens-before relation to guarantee consistent behavior across platforms.

2. Explain volatile's guarantees

State that a volatile write happens-before any subsequent volatile read of the same variable, ensuring visibility and preventing certain reorderings. Also note that volatile accesses are not atomic for compound operations.

3. Contrast with synchronized and final

Briefly compare volatile with synchronized (which provides mutual exclusion and visibility) and final fields (which have special initialization safety guarantees). This shows depth of understanding.

4. Provide a practical example

Use a simple example like a volatile boolean flag to stop a thread, or a volatile reference to an immutable object, to illustrate correct usage and common pitfalls.

5. Summarize trade-offs and best practices

Conclude with when to use volatile (simple flags, status indicators) versus when to use locks or atomic classes, emphasizing correctness and performance considerations.

Key Points to Mention

  • Happens-before relationship and its role in the JMM
  • Volatile guarantees visibility and ordering, but not atomicity
  • Difference between volatile and synchronized
  • Reordering and memory barriers (e.g., load/store fences)
  • Use cases: flags, status indicators, and lock-free algorithms
  • Common pitfalls: assuming volatile makes compound actions atomic

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

When would you use atomic classes like AtomicInteger versus a lock-based approach?

Technical Trade-offsSystem Design
Author's notes

Pretty comfortable here.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying that atomic classes are best for simple, single-variable operations where you need lock-free thread safety and high throughput. Contrast this with lock-based approaches, which are necessary for compound actions, multiple variables, or when you need condition-based waiting. Emphasize that the choice depends on the complexity of the critical section and contention levels.

Pro tip: Mention that atomic classes use CAS (Compare-And-Swap) and can suffer from ABA problems and high contention overhead, so they aren't a silver bullet. Also, note that in real systems, you often combine both: use atomics for counters and locks for coordinating complex state changes.

1. Define the problem scope

Identify whether the shared state involves a single variable or multiple variables, and whether the operations are simple (e.g., increment) or compound (e.g., check-then-act).

2. Evaluate atomic suitability

If the operation is a single-variable, non-blocking update, atomic classes like AtomicInteger are ideal because they avoid locking overhead and provide better scalability under low to moderate contention.

3. Assess lock-based needs

If you need to update multiple variables atomically, perform compound actions, or use condition variables for waiting/notification, a lock (e.g., ReentrantLock or synchronized) is required.

4. Consider contention and performance

Under high contention, atomics can spin and waste CPU, while locks can cause context switching; choose based on expected contention and performance requirements.

5. Summarize with a trade-off

Conclude that atomics are for simple, lock-free updates, while locks are for complex critical sections, and mention that hybrid approaches or higher-level abstractions (e.g., LongAdder) may be better in some cases.

Key Points to Mention

  • Atomic classes use CAS (Compare-And-Swap) for lock-free thread safety.
  • Locks are needed for compound actions or multiple variables to ensure atomicity.
  • Atomics avoid blocking and context switching, improving scalability under low contention.
  • Under high contention, atomics can cause busy-waiting and performance degradation.
  • Locks provide fairness, condition variables, and support for complex synchronization.
  • Alternatives like LongAdder or StampedLock can be better for specific scenarios.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q5

What concurrent collections does Java provide and what are the trade-offs between them?

Technical Trade-offsAlgorithms & Data Structures
Author's notes

ConcurrentHashMap vs Collections.synchronizedMap is the obvious one and I covered it fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by categorizing Java's concurrent collections into groups like concurrent maps, queues, and sets, then discuss the trade-offs in terms of thread-safety, performance, and consistency. Focus on how each collection achieves concurrency and when to use which, providing examples from real-world scenarios.

Pro tip: Mention that while ConcurrentHashMap is often the go-to, its weakly consistent iterators mean you might not see the latest updates, which is a crucial trade-off in systems requiring strong consistency. Also, note that Weride's autonomous driving systems likely demand high throughput and low latency, so emphasizing lock-free algorithms and non-blocking collections can show alignment with their domain.

1. Categorize the collections

Group Java's concurrent collections into maps (ConcurrentHashMap, ConcurrentSkipListMap), queues (BlockingQueue implementations like ArrayBlockingQueue, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueue, DelayQueue, LinkedTransferQueue), and sets (ConcurrentSkipListSet, CopyOnWriteArraySet).

2. Explain concurrency mechanisms

For each category, describe how thread-safety is achieved: e.g., ConcurrentHashMap uses lock striping and CAS, BlockingQueues use locks and conditions, CopyOnWrite collections use immutable snapshots.

3. Discuss trade-offs

Compare trade-offs: performance (throughput vs latency), consistency (weak vs strong), memory overhead, and scalability. For example, ConcurrentHashMap offers high concurrency but weakly consistent iterators, while CopyOnWriteArrayList is good for read-heavy scenarios but expensive for writes.

4. Provide use cases

Give practical examples of when to use each: e.g., ConcurrentHashMap for shared caches, BlockingQueue for producer-consumer patterns, CopyOnWriteArrayList for event listener lists.

5. Summarize and recommend

Conclude with a summary of key trade-offs and a recommendation based on typical requirements like high concurrency, low latency, or strong consistency.

Key Points to Mention

  • ConcurrentHashMap: lock striping, CAS, weakly consistent iterators, no null keys/values.
  • BlockingQueue implementations: ArrayBlockingQueue (bounded, fair locking), LinkedBlockingQueue (optionally bounded, higher throughput), SynchronousQueue (handoff), PriorityBlockingQueue (unbounded, priority order), DelayQueue (delayed elements).
  • CopyOnWriteArrayList/Set: snapshot iterators, no synchronization for reads, expensive writes, best for read-mostly scenarios.
  • ConcurrentSkipListMap/Set: sorted, lock-free via skip lists, weakly consistent iterators, O(log n) operations.
  • Trade-offs: throughput vs latency, consistency guarantees, memory footprint, scalability under contention.
  • Java 8+ enhancements: e.g., computeIfAbsent in ConcurrentHashMap, parallel bulk operations.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q6

Walk me through how a deadlock can occur in a multithreaded Java program and how you'd prevent or detect it.

Root Cause AnalysisSystem Design
Author's notes

Gave a classic two-lock circular dependency example.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by defining deadlock and explaining the four necessary conditions (Coffman conditions). Then walk through a concrete Java example with two threads and two locks acquired in opposite order, and finally discuss prevention strategies (lock ordering, timeouts, deadlock detection via thread dumps) and how you'd apply them in practice.

Pro tip: Mention that in production you'd use jstack or ThreadMXBean.findDeadlockedThreads() to detect deadlocks, and that prevention via consistent lock ordering is usually preferred over detection because it avoids the performance overhead of monitoring.

1. Define deadlock and its conditions

Explain that deadlock occurs when two or more threads are blocked forever, each waiting for a lock held by another. List the four Coffman conditions: mutual exclusion, hold and wait, no preemption, and circular wait.

2. Illustrate with a Java example

Describe a scenario with two threads: Thread A locks resource 1 then tries to lock resource 2, while Thread B locks resource 2 then tries to lock resource 1. Show how this creates a circular wait and deadlock.

3. Explain prevention techniques

Discuss strategies to prevent deadlock: acquiring locks in a consistent global order, using tryLock with timeout, avoiding nested locks, and using higher-level concurrency utilities like java.util.concurrent.

4. Describe detection and recovery

Explain how to detect deadlocks using thread dumps (jstack), ThreadMXBean.findDeadlockedThreads(), or JConsole. Mention recovery options like restarting the affected threads or the application.

5. Tie to real-world practices

Emphasize that in production, prevention is preferred. Mention code reviews, static analysis tools, and designing lock-free algorithms where possible.

Key Points to Mention

  • Coffman conditions: mutual exclusion, hold and wait, no preemption, circular wait
  • Java synchronized blocks and ReentrantLock
  • Lock ordering to prevent circular wait
  • tryLock with timeout to avoid indefinite blocking
  • Thread dumps and ThreadMXBean for detection
  • java.util.concurrent utilities like ConcurrentHashMap and atomic variables

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q7

How do race conditions differ from visibility issues, and how do you address each?

Technical Trade-offsRoot Cause Analysis
Author's notes

Honestly spent too long conflating the two before separating them out.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clearly defining race conditions and visibility issues, emphasizing that they are distinct concurrency problems with different root causes. Then, for each, explain common mitigation techniques and how they address the underlying issue. Finally, tie it back to practical experience, perhaps mentioning tools or patterns you've used.

Pro tip: Mention that while locks can solve both, they do so differently: locks provide mutual exclusion to prevent races, and also establish happens-before relationships to ensure visibility. This shows deep understanding.

1. Define race conditions

Explain that a race condition occurs when multiple threads access shared data concurrently and the outcome depends on timing, leading to incorrect results. Emphasize the need for atomicity.

2. Define visibility issues

Explain that visibility issues occur when changes made by one thread are not visible to other threads due to caching or compiler optimizations, leading to stale data. Emphasize the need for memory synchronization.

3. Contrast root causes

Highlight that race conditions are about interleaving of operations (atomicity), while visibility issues are about memory consistency (ordering and propagation of writes).

4. Address race conditions

Discuss techniques like locks, atomic variables, and transactional memory to ensure mutual exclusion or atomicity.

5. Address visibility issues

Discuss techniques like volatile variables, memory barriers, and synchronization constructs that enforce happens-before relationships.

Key Points to Mention

  • Race conditions require atomicity; visibility issues require memory synchronization.
  • Locks solve both but in different ways: mutual exclusion vs. memory barriers.
  • Atomic variables (e.g., AtomicInteger) address race conditions but not visibility of other variables.
  • Volatile ensures visibility but not atomicity.
  • Happens-before relationship is key to visibility.
  • Real-world examples: counter increment (race), flag update (visibility).

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.