Java Coding Problems
10 Object-Oriented, Concurrency & Stream API Challenges
Document Overview: 10 carefully curated coding challenges designed to evaluate knowledge of language idioms,
concurrency, memory management, algorithmic thinking, and core standard libraries in Java.
Problem 1: Custom Thread Pool Executor HARD
Implement a custom thread pool execution framework from scratch in Java without using
`[Link]`. Implement worker threads, a bounded blocking task queue, and shutdown
capabilities.
FUNCTION SIGNATURE / INTERFACE:
public class CustomThreadPool {
public CustomThreadPool(int numThreads, int queueCapacity) { ... }
public void execute(Runnable task) throws InterruptedException { ... }
public void shutdown() { ... }
}
INPUT / CONTEXT:
Pool size = 4, Task queue capacity = 10, submit 20 Runnable tasks.
EXPECTED OUTPUT:
All tasks executed sequentially/concurrently across the 4 threads with clean shutdown.
CONSTRAINTS & GUIDANCE:
Use standard Java synchronization primitives (`synchronized`, `wait()`, `notifyAll()`, or `ReentrantLock`).
Page 1 of 6
Problem 2: Reactive Stream Data Transformer using CompletableFuture MEDIUM
Given an asynchronous data source returning user IDs, fetch user details and their order history concurrently
using Java 8+ `CompletableFuture`, combining and filtering results.
FUNCTION SIGNATURE / INTERFACE:
public CompletableFuture> fetchHighValueSummaries(List userIds)
INPUT / CONTEXT:
List of user IDs: [101, 102, 103]
EXPECTED OUTPUT:
List of `UserSummary` objects containing aggregated order values above $100.
CONSTRAINTS & GUIDANCE:
Must process calls asynchronously without blocking main thread (`[Link]()`).
Problem 3: Top K Frequent Elements using Priority Queue MEDIUM
Given a non-empty array of integers, return the `k` most frequent elements using a Min-Heap (`PriorityQueue`).
FUNCTION SIGNATURE / INTERFACE:
public int[] topKFrequent(int[] nums, int k)
INPUT / CONTEXT:
nums = [1,1,1,2,2,3], k = 2
EXPECTED OUTPUT:
[1, 2]
CONSTRAINTS & GUIDANCE:
Time complexity must be better than O(N log N), targeting O(N log k).
Page 2 of 6
Problem 4: LCA of Deepest Leaves in Binary Tree MEDIUM
Given the root of a binary tree, return the lowest common ancestor (LCA) of its deepest leaves.
FUNCTION SIGNATURE / INTERFACE:
public TreeNode lcaDeepestLeaves(TreeNode root)
INPUT / CONTEXT:
TreeNode root = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4]
EXPECTED OUTPUT:
Node with value 2 (ancestor of leaves 7 and 4)
CONSTRAINTS & GUIDANCE:
Single-pass post-order depth traversal required.
Problem 5: Generic High-Performance Circular Ring Buffer MEDIUM
Design a lock-free or bounded thread-safe fixed-capacity Ring Buffer using Java Generics `RingBuffer` for low-
latency producer-consumer queues.
FUNCTION SIGNATURE / INTERFACE:
public class RingBuffer {
public RingBuffer(int capacity) { ... }
public boolean offer(T item) { ... }
public T poll() { ... }
}
INPUT / CONTEXT:
Capacity = 3, produce(A), produce(B), produce(C), consume() -> A, produce(D).
EXPECTED OUTPUT:
Buffer states maintained accurately without array index out of bounds.
CONSTRAINTS & GUIDANCE:
Must handle buffer wrap-around correctly with atomic or synchronized pointers.
Page 3 of 6
Problem 6: Stream API Graph Grouping & Summarization EASY
Given a list of `Transaction` objects (id, category, amount, status), use the Java Stream API
(`[Link]`, `[Link]`) to compute aggregate summary statistics per
category for completed transactions.
FUNCTION SIGNATURE / INTERFACE:
public Map summarizeByCategory(List txs)
INPUT / CONTEXT:
List of Transaction instances.
EXPECTED OUTPUT:
Map summarizing totals, average, count per category.
CONSTRAINTS & GUIDANCE:
Must be implemented purely via Java 8+ Functional Streams.
Problem 7: Custom Annotation Driven Validation Framework HARD
Create a runtime annotation `@NotNullOrEmpty` and `@MinVal(value)` and write a validator class that uses Java
Reflection API to inspect object fields and validate constraints.
FUNCTION SIGNATURE / INTERFACE:
public class AnnotationValidator {
public static List validate(Object obj) throws IllegalAccessException { ... }
}
INPUT / CONTEXT:
Object instance `User(name=null, age=15)` where `@MinVal(18)` on age.
EXPECTED OUTPUT:
List of validation error string messages.
CONSTRAINTS & GUIDANCE:
Must use standard `[Link]` API.
Page 4 of 6
Problem 8: Serialize and Deserialize Binary Tree HARD
Design an algorithm to serialize a binary tree into a single String representation and deserialize that string back
into the exact original binary tree topology.
FUNCTION SIGNATURE / INTERFACE:
public String serialize(TreeNode root);
public TreeNode deserialize(String data);
INPUT / CONTEXT:
Tree: [1, 2, 3, null, null, 4, 5]
EXPECTED OUTPUT:
Serialized string and reconstructed root matching original tree.
CONSTRAINTS & GUIDANCE:
Do not use class member/global variables to store state.
Problem 9: Longest Palindromic Substring MEDIUM
Given a string `s`, return the longest palindromic substring in `s` using dynamic programming or expand-around-
center approach.
FUNCTION SIGNATURE / INTERFACE:
public String longestPalindrome(String s)
INPUT / CONTEXT:
s = "babad"
EXPECTED OUTPUT:
"bab" (or "aba")
CONSTRAINTS & GUIDANCE:
Time complexity O(N^2), Space complexity O(1) if expanding around center.
Page 5 of 6
Problem 10: Expression Evaluator with Precedence HARD
Implement an expression evaluator in Java that parses and calculates arithmetic mathematical expressions
containing non-negative integers, `+`, `-`, `*`, `/`, and parentheses `()`.
FUNCTION SIGNATURE / INTERFACE:
public int calculate(String s)
INPUT / CONTEXT:
s = "3 + (2 * 4) / ( 1 + 1 )"
EXPECTED OUTPUT:
CONSTRAINTS & GUIDANCE:
Do not use built-in JavaScript/script engines. Solve using Stacks or Shunting-yard algorithm.
Page 6 of 6