Course contents

Parallel streams & Spliterator: interview questions & scenarios

Premium9 questions · 18 what-if follow-ups

These are the parallel-stream questions I use to test whether you can reason about work splitting, ordering and shared pools under pressure. Answer it out loud first, then open the model answer, and revisit the lessons when a detail feels fuzzy: Parallel streams: when they help and hurt, Custom thread pools for parallel streams and Spliterator explained.

Question 1. ConceptCoreAsked very often

When do parallel streams help, and when do they usually hurt?

Tests NQ model, splittability, CPU work, merge cost

Show answerHide answer2 follow-ups

Model answer

A parallel stream helps when the source splits cheaply, the input is large enough, each element has meaningful CPU work, the operation is stateless, and the terminal operation can merge partial results cheaply. Arrays, ArrayList and primitive ranges are good sources. LinkedList, Stream.iterate and I/O-backed sources are poor candidates.

The useful interview shorthand is the NQ model: N elements times Q cost per element should comfortably exceed about 10,000 small operations before I even expect a win. That is not a law. It is a smell test that keeps you from parallelizing 50 strings and wondering why it got slower.

Parallel streams usually hurt for small inputs, cheap lambdas, blocking I/O, shared mutable state, boxing-heavy pipelines, expensive groupingBy merges, and ordered operations like findFirst, limit and forEachOrdered.

How I’d say it in the interview

"I use parallel streams for CPU-bound work over splittable in-memory data, with pure functions and cheap merging. I avoid them for small lists, blocking calls, shared mutation and operations that need strict encounter order. Then I measure with JMH or production profiling."

Signals depth

Mentioning that parallel() is only a flag. The terminal operation drives splitting through the source Spliterator.

Red flags

"Parallel is faster because it uses more threads" without naming source splitting, work per element or merge cost.

Follow-ups interviewers ask next

  1. What if the input is an ArrayList of 5,000,000 numbers?

    That is a plausible source. I would still prefer primitive streams such as IntStream when possible, keep the operation pure, and measure sequential versus parallel under the same workload.

  2. What if the input is a LinkedList?

    I would be skeptical. Splitting is poor because the structure is pointer-based, so workers can spend too much time dividing work unevenly.

Question 2. What does it print?CoreAsked very often

What can this parallel findAny program print?

Tests findAny, encounter order, parallel short-circuiting

Show answerHide answer2 follow-ups

Model answer

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Optional<String> result = List.of("A", "B", "C", "D")
                .parallelStream()
                .filter(s -> !s.equals("A"))
                .findAny();

        System.out.println(result.orElse("none"));
    }
}

It can print B, C or D. On many Java 21 runs you may see one value repeatedly, but the contract does not promise the first matching element for findAny, especially in parallel.

If the requirement is first in list order, use findFirst() and accept the ordering cost. If the requirement is any eligible value, findAny() is the honest method and can short-circuit with less coordination.

How I’d say it in the interview

"This prints some matching element, not necessarily B. findAny lets a parallel worker win the race. If business order matters, I say findFirst; if any match proves the condition, I prefer findAny."

Signals depth

Separating the API contract from the value you happened to observe on your laptop.

Red flags

Saying it must print B because B is the first element after filtering.

Follow-ups interviewers ask next

  1. What if I remove parallelStream()?

    Sequential findAny() often behaves like findFirst() in practice, but I still would not describe its contract as "first". Use the method that says what you mean.

  2. What if I call .unordered() before findAny()?

    You give the stream library permission to ignore encounter order. That can help only when the result remains correct without order.

Question 3. DebuggingCoreAsked very often

This code adds to an ArrayList inside parallelStream().forEach. What is the bug?

Tests shared mutable state, non-interference, forEach

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 4. ScenarioSeniorAsked very often

A web app changed orders.stream() to orders.parallelStream() for HTTP calls and everything slowed down. Why?

Tests blocking I/O, common pool, web app contention, alternatives

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 5. ConceptSeniorCommonly asked

How do you run a parallel stream in a custom thread pool, and what is the caveat?

Tests ForkJoinPool submit pattern, spec caveat, lifecycle

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 6. ConceptCoreCommonly asked

Explain how a Spliterator affects parallel stream performance.

Tests trySplit, estimateSize, characteristics, source quality

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 7. ConceptCoreAsked very often

What is the difference between findFirst and findAny in a parallel pipeline?

Tests ordering, short-circuiting, encounter order

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 8. DebuggingSeniorCommonly asked

This parallel reduction sometimes gives a strange answer. What went wrong?

Tests identity, associativity, reduction combiner

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Question 9. DesignSeniorCommonly asked

How would you decide whether to add .parallel() to an existing production stream?

Tests measurement, correctness, operational risk, rollback

Model answer, how to say it, 2 follow-ups: unlock with the Java Pass

Premium question set

Keep reading with the Java Pass

The rest of this question set is part of Java One-Stop Interview Prep: in-depth explanations, diagrams, model answers and how to say them in the interview.

From ₹199 · one-time payment · No auto-renewal

Lessons in Parallel streams & Spliterator