Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Refactor for Lower Memory

MediumCoding00:00
Practice interviewer
In session
5 left
00:00

Your question is Refactor for Lower Memory. Take a moment with it on the right.

Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).

You need to log in / sign up to chat or submit.

Problem

At Rubrik, this class reads a backup snapshot file in fixed-size chunks and computes a checksum for each chunk before it's written to cloud storage. It's used on every backup job, and on large snapshots (hundreds of gigabytes) it has been driving the backup worker's memory usage high enough to trigger OOM kills.

public class ChunkChecksummer {
    public List<byte[]> chunks = new ArrayList<>();
    public List<String> checksums = new ArrayList<>();

    public void process(String filePath, int chunkSize) throws IOException {
        byte[] fileBytes = Files.readAllBytes(Paths.get(filePath));

        int offset = 0;
        while (offset < fileBytes.length) {
            int end = Math.min(offset + chunkSize, fileBytes.length);
            byte[] chunk = Arrays.copyOfRange(fileBytes, offset, end);
            chunks.add(chunk);
            checksums.add(computeChecksum(chunk));
            offset = end;
        }
    }

    private String computeChecksum(byte[] chunk) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(chunk);
            StringBuilder sb = new StringBuilder();
            for (byte b : hash) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}

Explain what you would change about this class to bring its memory usage down on large snapshot files, and why each change matters.