Scrivio Interview library

I/O and NIO

39 questions in 14 subtopics.

Byte and character streams

ConceptFoundations6 min read
What is the difference between InputStream/OutputStream and Reader/Writer? How does InputStreamReader connect them, and what goes wrong if you do not pass a charset?
CodingFoundations7 min read
Find the problems:
FileInputStream in = new FileInputStream(f);
byte[] buf = new byte[1024];
in.read(buf);
String s = new String(buf);
in.close();
DebuggingIntermediate7 min read
A service's file export works on developers' Macs but produces garbled non-ASCII names on a Windows batch server running Java 11. Explain the cause and how upgrading to Java 21 changes it.
LeadershipAdvanced6 min read
After moving to Java 18 or later, the default charset is UTF-8 everywhere. Your fleet has many services that once relied on platform defaults. How do you plan the transition and what coding standard results from it?
LeadershipPrincipal6 min read
Java I/O has gone through java.io, NIO in 1.4, NIO.2 in 7, the UTF-8 default in 18 and virtual threads in 21. What does that history suggest about how your organisation should write I/O code that lasts?

Buffered I/O

ConceptFoundations6 min read
Why does wrapping a FileInputStream in a BufferedInputStream speed up reading a file byte by byte? What is the default buffer size and when is buffering pointless?
DebuggingIntermediate6 min read
A log shipper writes lines through a BufferedWriter and the last lines of each file are missing after the process exits. What is wrong, and what else should a reviewer check?

NIO buffers and channels

ConceptFoundations6 min read
Explain ByteBuffer position, limit, capacity and flip(). Why do people get empty results after writing data into a buffer and then reading from it?
ConceptIntermediate7 min read
What is the difference between a heap ByteBuffer and a direct ByteBuffer? When is each the better choice, and what operational problems can direct buffers cause?

Files and Path API

ConceptFoundations6 min read
What are the advantages of java.nio.file.Files and Path over java.io.File? Give three methods that File lacks.
CodingFoundations6 min read
Using the Files and Path API, write code that reads all lines of a large log file, counts lines containing ERROR, and does not load the whole file into memory.
DebuggingIntermediate6 min read
A scheduled job uses Files.list(dir) every minute and after a few days fails with Too many open files. What is wrong?
ScenarioIntermediate7 min read
A service writes a JSON state file that other processes read. Occasionally readers see a half-written file. How do you write the file safely?
LeadershipPrincipal6 min read
Containers made local disks short-lived, and object storage replaced many shared file systems. How has this changed the role of file I/O in Java services, and what guidance would you give teams over the next years?

Memory-mapped files

InternalsIntermediate7 min read
How do memory-mapped files work in Java with FileChannel.map? What are their benefits and their known problems?
DesignAdvanced7 min read
A team proposes memory-mapping a 40 GB file for a low-latency lookup service running in containers. What do you check before approving it?
LeadershipPrincipal6 min read
Several teams want to build custom storage engines in Java using memory-mapped files and off-heap memory for speed. Over a multi-year horizon, when is building such infrastructure justified, and when should the company buy or adopt an existing engine?

WatchService

CodingFoundations6 min read
Write code that watches a directory for new files using WatchService and processes each one. What are common mistakes?
ScenarioIntermediate6 min read
A WatchService-based importer works on Linux but misses or delays files on macOS and on a network-mounted volume. Why, and what would you use instead?

Asynchronous channels

ConceptIntermediate7 min read
Explain AsynchronousFileChannel and AsynchronousSocketChannel. How do the Future and CompletionHandler styles differ, and why are they rarely used directly in modern services?

Selectors and non-blocking I/O

InternalsIntermediate8 min read
How does a Selector enable one thread to serve many connections? Describe the main loop and the role of SelectionKey interest sets.
DesignAdvanced7 min read
With virtual threads available, does your organisation still need non-blocking I/O frameworks such as Netty or WebFlux for new services? How would you decide per service?
LeadershipPrincipal6 min read
Java went from blocking thread-per-connection I/O, to NIO selectors and reactive frameworks, to virtual threads that make blocking cheap again. How would you explain this history to your leadership, and what does it mean for platform investments?

Zero-copy transfer

InternalsIntermediate7 min read
What does FileChannel.transferTo do, and why can it be faster than reading into a buffer and writing to a socket? What limits does it have?
ScenarioAdvanced6 min read
Your file download service streams large files through Spring MVC and CPU is high. What options for reducing copies would you evaluate, across the stack?

Console input and Scanner

CodingFoundations6 min read
A command-line tool must read up to 10 million integers from standard input as fast as possible (one per line). Why is Scanner slow here, and what would you use instead?
DebuggingFoundations5 min read
This code skips the name input:
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
String name = sc.nextLine();
Explain why and fix it.
ScenarioIntermediate6 min read
An internal Java CLI prompts for a database password with Scanner and echoes it on screen, and when run in CI it hangs waiting for input. How would you redesign its input handling?

Byte order and endianness

ConceptFoundations5 min read
What is endianness, which byte order does Java use by default for DataOutputStream and ByteBuffer, and how would you read a little-endian 32-bit int from a file header?
DebuggingIntermediate6 min read
A Java service parses a binary file produced by a C program on x86 and all numbers come out huge and wrong, but small byte values are fine. After switching to little-endian the integers are right, yet 64-bit timestamps at offset 6 are still wrong. What do you check?
DesignAdvanced6 min read
Several Java services exchange binary data with C++ systems and IoT devices using hand-written byte layouts, and every change causes production incidents. What would you standardise?

Zip archives

CodingFoundations6 min read
Write Java code that unzips an uploaded archive into a target directory safely. What attack must you guard against?
ScenarioIntermediate7 min read
A Spring Boot endpoint builds a ZIP of 2,000 PDFs from object storage for download and the pods run out of memory. How do you redesign it?
LeadershipAdvanced6 min read
Security found Zip Slip and zip bomb vulnerabilities in four different services that each handle uploaded archives. How do you fix this at the organisation level?

CSV parsing

DebuggingFoundations6 min read
A developer parses CSV with line.split(",") and imports go wrong for some rows. Give three kinds of input that break it and what you would use instead.
DesignIntermediate7 min read
Partners upload CSV files of up to 5 GB with 30 million rows to import into PostgreSQL. Design the import in a Spring Boot service, including validation and error reporting.
LeadershipPrincipal6 min read
CSV files remain the main way your company exchanges data with hundreds of partners, despite repeated data quality incidents. As the senior technical leader, would you invest in replacing CSV, and how?

Properties files

ConceptFoundations7 min read
How does java.util.Properties read a .properties file, and what pitfalls exist with encoding, escaping and whitespace?
ScenarioIntermediate6 min read
A Spring Boot service has 14 .properties files across profiles and environments, and production behaviour differs from staging because of an unexpected override. How do you find which value wins and simplify the setup?