Chaturmind
LearnDSASystem DesignInterview PrepDevOpsEngineering GrowthBlog
Start learning
Chaturmind

Structured learning paths for engineers who want to go deep. Written by practitioners.

Learn

  • Java
  • DSA
  • System Design
  • Spring Boot
  • AI / ML
  • DevOps
  • Engineering Growth
  • Java Interview Prep

Company

  • Blog
  • Contact

Legal

  • Privacy Policy
  • Terms of Service

© 2026 Chaturmind. All rights reserved.

Built for engineers who want to go deep.


← Java Interview Prep: 2–5 Years

Revise the Fresher Tier

  • Revise: Core Java Fundamentals (Fresher Tier)
  • Revise: Collections, Threads, Java 8 & Coding (Fresher Tier)
  • Revise: Spring Core, Spring Boot & Spring MVC (Fresher Tier)
  • Revise: Hibernate, SQL, Microservices, Maven & Git (Fresher Tier)

Intermediate Core Java

  • Intermediate OOP & Language Features — Interview Questions
  • Enums, Generics, Pass-by-Value & Imports — Interview Questions
  • Collections Internals & Scenarios — Interview Questions
  • JVM, Memory & Class Loading — Interview Questions
  • Exception Design & Serialization Edge Cases — Interview Questions

Java 8 Deep Dive & Stream Coding

  • Lambdas, Functional Interfaces & Method References — Interview Questions
  • Default Methods, Backward Compatibility & Optional — Interview Questions
  • Stream API Internals (Part 1) — Interview Questions
  • Stream API Internals (Part 2) — Interview Questions
  • Stream Coding Problems (Level 2, Part 1) — Interview Questions
  • Stream Coding Problems (Level 2, Part 2) — Interview Questions

Concurrency & Multithreading

  • Synchronization, Locks & Deadlocks — Interview Questions
  • Executors, ThreadLocal & Concurrent Collections — Interview Questions

Spring Framework In Depth

  • Bean Lifecycle, Contexts & Circular Dependencies — Interview Questions
  • Spring Bean Conflicts, AOP, Batch & Injection Styles — Interview Questions

Spring Boot In Depth & Scenarios

  • Spring Boot Internals & Auto-Configuration — Interview Questions
  • Spring Boot Configuration, Profiles & Secrets — Interview Questions
  • Spring Boot Data, Transactions & Caching — Interview Questions
  • Spring Boot Testing, Error Pages & Deployment — Interview Questions
  • Spring Boot Performance, Scaling & Resilience — Interview Questions
  • Spring Boot Async, Events & Messaging — Interview Questions
  • Spring Boot Security Scenarios — Interview Questions
  • Actuator, AOP, Spring Cloud & Distributed Tracing — Interview Questions
  • External APIs, Files, GraphQL & WebFlux — Interview Questions

Spring Security

  • Spring Security — OAuth2, CORS, CSRF & Access Rules — Interview Questions
  • Spring Security — Passwords, Filter Chain, Sessions & Debugging — Interview Questions

Apache Kafka

  • Kafka Architecture, Topics & ZooKeeper vs KRaft — Interview Questions
  • Kafka Producers, Consumers, Reliability & Streams — Interview Questions
  • Kafka Security, Connect & Core Scenarios — Interview Questions
  • Kafka Production Scenarios — Interview Questions

Git, Maven & Gradle (Level II)

  • Git Workflows, Rebase & Conflict Handling — Interview Questions
  • Git Recovery, Hooks, Tags & Collaboration — Interview Questions
  • Maven Builds, Multi-Module Projects & Dependency Resolution — Interview Questions
  • Maven Profiles, settings.xml, Plugins & Quality Gates — Interview Questions
  • Gradle Fundamentals, Migration & Dependencies — Interview Questions
  • Gradle Performance, Plugins, Testing & Publishing — Interview Questions

Deployment & CI/CD

  • Sessions, CI/CD Pipelines, Rollbacks & Secrets — Interview Questions
  • Migrations, Zero-Downtime Releases, Containers & Monitoring — Interview Questions

JUnit 5 & Mockito

  • JUnit 5 — Interview Questions
  • Mockito Basics — Interview Questions
  • Mockito Advanced & Tricky Questions — Interview Questions
Chaturmind
← Java Interview Prep: 2–5 Years

Revise the Fresher Tier

  • Revise: Core Java Fundamentals (Fresher Tier)
  • Revise: Collections, Threads, Java 8 & Coding (Fresher Tier)
  • Revise: Spring Core, Spring Boot & Spring MVC (Fresher Tier)
  • Revise: Hibernate, SQL, Microservices, Maven & Git (Fresher Tier)

Intermediate Core Java

  • Intermediate OOP & Language Features — Interview Questions
  • Enums, Generics, Pass-by-Value & Imports — Interview Questions
  • Collections Internals & Scenarios — Interview Questions
  • JVM, Memory & Class Loading — Interview Questions
  • Exception Design & Serialization Edge Cases — Interview Questions

Java 8 Deep Dive & Stream Coding

  • Lambdas, Functional Interfaces & Method References — Interview Questions
  • Default Methods, Backward Compatibility & Optional — Interview Questions
  • Stream API Internals (Part 1) — Interview Questions
  • Stream API Internals (Part 2) — Interview Questions
  • Stream Coding Problems (Level 2, Part 1) — Interview Questions
  • Stream Coding Problems (Level 2, Part 2) — Interview Questions

Concurrency & Multithreading

  • Synchronization, Locks & Deadlocks — Interview Questions
  • Executors, ThreadLocal & Concurrent Collections — Interview Questions

Spring Framework In Depth

  • Bean Lifecycle, Contexts & Circular Dependencies — Interview Questions
  • Spring Bean Conflicts, AOP, Batch & Injection Styles — Interview Questions

Spring Boot In Depth & Scenarios

  • Spring Boot Internals & Auto-Configuration — Interview Questions
  • Spring Boot Configuration, Profiles & Secrets — Interview Questions
  • Spring Boot Data, Transactions & Caching — Interview Questions
  • Spring Boot Testing, Error Pages & Deployment — Interview Questions
  • Spring Boot Performance, Scaling & Resilience — Interview Questions
  • Spring Boot Async, Events & Messaging — Interview Questions
  • Spring Boot Security Scenarios — Interview Questions
  • Actuator, AOP, Spring Cloud & Distributed Tracing — Interview Questions
  • External APIs, Files, GraphQL & WebFlux — Interview Questions

Spring Security

  • Spring Security — OAuth2, CORS, CSRF & Access Rules — Interview Questions
  • Spring Security — Passwords, Filter Chain, Sessions & Debugging — Interview Questions

Apache Kafka

  • Kafka Architecture, Topics & ZooKeeper vs KRaft — Interview Questions
  • Kafka Producers, Consumers, Reliability & Streams — Interview Questions
  • Kafka Security, Connect & Core Scenarios — Interview Questions
  • Kafka Production Scenarios — Interview Questions

Git, Maven & Gradle (Level II)

  • Git Workflows, Rebase & Conflict Handling — Interview Questions
  • Git Recovery, Hooks, Tags & Collaboration — Interview Questions
  • Maven Builds, Multi-Module Projects & Dependency Resolution — Interview Questions
  • Maven Profiles, settings.xml, Plugins & Quality Gates — Interview Questions
  • Gradle Fundamentals, Migration & Dependencies — Interview Questions
  • Gradle Performance, Plugins, Testing & Publishing — Interview Questions

Deployment & CI/CD

  • Sessions, CI/CD Pipelines, Rollbacks & Secrets — Interview Questions
  • Migrations, Zero-Downtime Releases, Containers & Monitoring — Interview Questions

JUnit 5 & Mockito

  • JUnit 5 — Interview Questions
  • Mockito Basics — Interview Questions
  • Mockito Advanced & Tricky Questions — Interview Questions
HomeLearnJava Interview PrepJava Interview Prep: 2–5 YearsJava 8 Deep Dive & Stream Coding
✓ FreeIntermediate· 6 min read

Stream Coding Problems (Level 2, Part 1) — Interview Questions

Evens and partitioning, numbers starting with 1, duplicates (three correct ways), first element, count, max, first non-repeated and first repeated character, ascending and descending sort, and contains-duplicate — with the bugs in common answers fixed.

Published September 25, 2026


How to use this lesson

These are the stream problems asked in live coding at the 2–5 year level. Interviewers look for three things: a correct pipeline, no side effects hidden in lambdas, and awareness of edge cases (empty input, case, whitespace). Several popular answers online mutate a shared HashSet inside filter. It works in a demo, but it's a hidden bug. The versions below avoid it.

Sample data:

List<Integer> nums = List.of(10, 15, 8, 49, 25, 98, 98, 32, 15);

Q1. Find all the even numbers in a list.

List<Integer> evens = nums.stream().filter(n -> n % 2 == 0).toList();              // [10, 8, 98, 98, 32]

Map<Boolean, List<Integer>> evenOdd = nums.stream()
        .collect(Collectors.partitioningBy(n -> n % 2 == 0));                     // {false=[15,49,25,15], true=[10,8,98,98,32]}

Key points to cover:

  • partitioningBy returns both groups in one pass, and always includes both keys.
  • For an int[], use Arrays.stream(arr).filter(...).toArray() to avoid boxing.

Learn it in depth → Collectors

Q2. Find the numbers that start with the digit 1.

List<Integer> startsWith1 = nums.stream()
        .filter(n -> String.valueOf(n).startsWith("1"))
        .toList();                                                                 // [10, 15, 15]

Key points to cover:

  • Negative numbers start with "-". Use String.valueOf(Math.abs(n)) if "−15 starts with 1" should count.
  • A numeric alternative without strings: divide by 10 until the value is below 10, then compare with 1.

Q3. Find the duplicate elements in a list.

Correct, side-effect-free versions:

// 1) group and count: duplicates listed once, with first-seen order kept
List<Integer> duplicates = nums.stream()
        .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
        .entrySet().stream()
        .filter(e -> e.getValue() > 1)
        .map(Map.Entry::getKey)
        .toList();                                                                 // [15, 98]

// 2) frequency check: simple, but O(n²)
List<Integer> dups2 = nums.stream().filter(n -> Collections.frequency(nums, n) > 1).distinct().toList();

// removing duplicates instead (the other half of the question)
List<Integer> unique = nums.stream().distinct().toList();                          // order preserved

Common trap: Set<Integer> seen = new HashSet<>(); nums.stream().filter(n -> !seen.add(n)). The lambda mutates external state, so it's not safe for parallel streams, and it outputs an element once per extra occurrence (a value appearing three times is reported twice). Use grouping instead.

Q4. Find the first element of a list.

Optional<Integer> first = nums.stream().findFirst();                               // Optional[10]
first.ifPresent(System.out::println);

Key points to cover:

  • For a List, list.isEmpty() ? null : list.get(0) is simpler. On Java 21, use list.getFirst(), which throws on an empty list. Use streams when the first element comes after filtering.

Q5. Count the elements in a list.

long total = nums.stream().count();                                                // 9
long distinctCount = nums.stream().distinct().count();                             // 7

Key points to cover:

  • For a plain collection, nums.size() is the right answer. count() makes sense after filter or distinct.

Q6. Find the maximum value.

int max = nums.stream().max(Comparator.naturalOrder()).orElseThrow();              // 98
int max2 = nums.stream().mapToInt(Integer::intValue).max().orElse(Integer.MIN_VALUE);

Common trap: calling .get() on the Optional without thinking about the empty list. Choose orElseThrow with a clear message, or a documented default.

Q7. Find the first non-repeated character in a string.

String input = "Java articles are Awesome";

Optional<Character> firstUnique = input.chars()
        .mapToObj(c -> Character.toLowerCase((char) c))
        .filter(Character::isLetter)                                                // ignore spaces (clarify with the interviewer)
        .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
        .entrySet().stream()
        .filter(e -> e.getValue() == 1)
        .map(Map.Entry::getKey)
        .findFirst();                                                               // Optional[j]

Key points to cover:

  • The LinkedHashMap keeps the first-occurrence order. With a plain HashMap the answer would be arbitrary.
  • The indexOf(c) == lastIndexOf(c) one-liner is O(n²). Mention it only as the simple version.
  • Clarify case sensitivity and whitespace before coding. Both change the answer.

Q8. Find the first repeated character in a string.

Short answer: Be precise about the definition. Usually it means "the first character, scanning left to right, that has already appeared earlier". A loop with a Set is the clearest correct answer. The grouping approach returns the first character (by first appearance) that repeats anywhere, which can differ.

static Optional<Character> firstRepeated(String s) {
    Set<Character> seen = new HashSet<>();
    for (char c : s.toLowerCase(Locale.ROOT).toCharArray()) {
        if (Character.isLetter(c) && !seen.add(c)) return Optional.of(c);        // first char seen twice
    }
    return Optional.empty();
}
// "Java Articles are Awesome" → 'a' (index 3 repeats the 'a' at index 1)

// stream version of "first character (by first appearance) that occurs more than once"
Optional<Character> firstThatRepeats = s.toLowerCase().chars()
        .mapToObj(c -> (char) c).filter(Character::isLetter)
        .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
        .entrySet().stream().filter(e -> e.getValue() > 1).map(Map.Entry::getKey).findFirst();

Key points to cover:

  • Saying "I'd use a plain loop here, because it short-circuits at the first repeat and needs no side effects in a lambda" is a strong answer, not a weak one.

Q9. Sort the values in ascending order.

List<Integer> ascending = nums.stream().sorted().toList();                          // [8, 10, 15, 15, 25, 32, 49, 98, 98]

Q10. Sort the values in descending order.

List<Integer> descending = nums.stream().sorted(Comparator.reverseOrder()).toList();
List<Integer> descUnique = nums.stream().distinct().sorted(Comparator.reverseOrder()).toList();

Key points to cover:

  • Collections.reverseOrder() and Comparator.reverseOrder() are equivalent here. For objects, Comparator.comparing(Employee::salary).reversed().

Q11. Contains duplicate: return true if any value appears at least twice.

static boolean containsDuplicate(int[] nums) {
    return Arrays.stream(nums).distinct().count() != nums.length;                  // simple and side-effect free
}

static boolean containsDuplicateFast(int[] nums) {
    Set<Integer> seen = new HashSet<>();
    for (int n : nums) if (!seen.add(n)) return true;                              // short-circuits at the first duplicate
    return false;
}
// [1, 2, 3, 1] → true    [1, 2, 3, 4] → false

Key points to cover:

  • Both are O(n) time and O(n) space.
  • The loop stops at the first duplicate. The distinct().count() version always scans everything.
  • Sorting first (Arrays.sort and then comparing neighbours) uses O(1) extra space, but O(n log n) time, and it modifies the input.

Learn it in depth → Group Anagrams

Follow-up questions this topic invites — and their answers

Q: How do you find the second-highest number with streams? A: nums.stream().distinct().sorted(Comparator.reverseOrder()).skip(1).findFirst(). It returns an Optional, which is empty when there are fewer than two distinct values.

Q: How do you count the occurrences of each element? A: nums.stream().collect(groupingBy(Function.identity(), counting())). Add a TreeMap::new supplier for sorted keys.

Q: Why avoid stateful lambdas in streams? A: The stream specification requires behavioural parameters to be non-interfering and (for most operations) stateless. Stateful lambdas give wrong or non-deterministic results in parallel streams, and make the pipeline's behaviour depend on evaluation order.

Q: How do you find the most frequent element? A: Group and count, then take entrySet().stream().max(Map.Entry.comparingByValue()). Decide how ties should be broken, and add a secondary comparator if needed.

Previous

Stream API Internals (Part 2) — Interview Questions

Next

Stream Coding Problems (Level 2, Part 2) — Interview Questions

AI Tutor

Lesson: Stream Coding Problems (Level 2, Part 1) — Interview Questions

Quick actions

AI responses can be inaccurate. Verify critical information.