DEV Community

Syed Mohammad Ibrahim
Syed Mohammad Ibrahim

Posted on

Code Review Exercises That Actually Work: Java Edition (Junior to Senior)

In Part 1 of this series, we explored a practical, respectful approach to engineering interviews - focusing on real-world skills like code reviews, GitHub project walkthroughs, and collaborative coding.

In this post, we'll dive deeper into the code review portion of the interview. You'll get:

  • A neutral Java code style guide to use during interviews
  • Three code review examples, tailored for junior, mid-level, and senior candidates
  • Tips for interviewers on how to run this part effectively

Why Code Reviews Belong in Interviews

Code reviews aren't just about syntax or formatting. They uncover how a candidate:

  • Thinks about design and readability
  • Communicates technical feedback
  • Prioritizes bugs, maintainability, and best practices
  • Balances tradeoffs and pragmatism

It's one of the most real-world ways to assess software engineers - and works across all levels of experience.


Java Style Guide for Code Review Exercises

This is a neutral guide to share with candidates during the exercise. It helps structure their review without giving away the "answers."

✅ Code Style & Structure

  • Use descriptive class and method names
  • Break down large methods into smaller, testable pieces
  • Follow consistent indentation and spacing

🔐 Exception Handling

  • Catch only specific exceptions
  • Don't suppress or swallow errors silently

🧼 Clean Code

  • Remove unnecessary code or comments
  • Use named constants instead of magic numbers/strings
  • Avoid duplicating logic

🔍 Input & Output

  • Minimize direct System.out.println()
  • Separate core logic from I/O when possible

🚦 Testability

  • Make code easy to unit test
  • Aim for pure functions and clear input/output boundaries

Junior-Level Code Review Example

Code Snippet:

public class UserDataProcessor {
    public static void process(String[] args) {
        for (int i = 0; i < args.length; i++) {
            String s = args[i];
            if (s.contains("@")) {
                System.out.println("Email: " + s);
            } else {
                try {
                    int n = Integer.parseInt(s);
                    System.out.println("Number: " + (n * 2));
                } catch (Exception e) {
                    System.out.println("Other: " + s);
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

What to Look For:

  • Vague method name (process)
  • Overuse of System.out.println()
  • Poor exception handling (catch (Exception e))
  • No input validation or testability

Optional Follow-up Prompt:

Now refactor the code to improve clarity and maintainability. Feel free to break it into smaller methods and add input validation.


Mid-Level Code Review Example

Code Snippet:

public class FileLogger {
    public void logMessage(String level, String message) {
        try {
            FileWriter writer = new FileWriter("log.txt", true);
            writer.write(level + ": " + message + "\n");
            writer.close();
        } catch (IOException e) {
            // ignore
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

What to Look For:

  • No resource management (FileWriter should use try-with-resources)
  • Swallowed exception with no logging or rethrow
  • No separation between formatting and writing
  • Hardcoded file path

Follow-up Prompt:

Refactor this to follow SOLID principles and Java best practices. Consider testability and error handling.


Senior-Level Code Review Example

Code Snippet:

public class NotificationService {
    private List<User> users;

    public void notifyUsers(String message) {
        for (User user : users) {
            if (user.getEmail() != null) {
                EmailSender.send(user.getEmail(), message);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

What to Look For:

  • Tight coupling to EmailSender (static call)
  • No abstraction/interface for notification method
  • No error handling or delivery guarantees
  • No logging or observability
  • Threading/scalability not considered

Follow-up Prompt:

Refactor this to support future extensions (e.g., SMS, push notifications). Discuss design tradeoffs and test strategy.


Tips for Interviewers

  • Use open-ended prompts after the review:
    • What would you change first?
    • Is this code testable? Why or why not?
    • Would this scale in a production system?
  • Match the complexity to the candidate's level
  • Don't judge by what they missed - focus on how they think, communicate, and justify decisions

Coming Up Next: Python, JavaScript, and More

This is just the beginning. Future posts will bring code review examples in other languages, helping teams run more effective interviews regardless of tech stack.

Top comments (1)

Collapse
 
ghost_engineer_2883a1ca0a profile image
Ghost Engineer

try this if you get stuck during the interview. its an AI co-pilot that solves the questions for you so you can focus on the more important part of the interview, the communication part. its also a really good study tool: ghostengineer.com