Member-only story
Java Interview Question: Remove Inactive Users Efficiently
Efficient User Cleanup in Large Datasets
Problem Description
Imagine you’re working on a user access control system for an application, and you need to clean up a list of users who have been marked as inactive. Your goal is to remove all inactive users (status = INACTIVE) from the list while keeping the operation efficient and minimizing memory usage.
The current solution creates a new list to store active users, which is memory inefficient for large lists.
- How would you modify the solution to improve memory usage by operating in place?
- What is the time complexity of your proposed solution, and how does it compare to the existing approach?
Subscribe to the Java newsletter to stay updated on future problems and solutions.
https://javabulletin.substack.com/subscribe
Existing Approach
- The existing approach Iterates over each customer filters only the active ones and then maps it to a new array list.
public static void main(String[] args) {
List<Customer> customers = new ArrayList<>();
Random random = new Random();
IntStream.rangeClosed(1, 100_000_000).forEach(i -> {…