Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started

Java Code Review and Improvements

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

Your question is Java Code Review and Improvements. 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

Morgan Stanley's trade settlement service processes batches of trades submitted by different desks throughout the day. A teammate wrote the class below to mark trades as settled and keep a running total, and it's now going through code review before it ships.

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class SettlementProcessor {

    private List<String> processedTradeIds = new ArrayList<>();
    private BigDecimal totalSettled = BigDecimal.ZERO;

    public void processBatch(List<Trade> trades) {
        for (Trade trade : trades) {
            try {
                if (processedTradeIds.contains(trade.getId())) {
                    continue;
                }
                if (trade.getStatus() == "SETTLED") {
                    continue;
                }
                settle(trade);
                processedTradeIds.add(trade.getId());
            } catch (Exception e) {
                // ignore and move on
            }
        }
    }

    private void settle(Trade trade) {
        totalSettled = totalSettled.add(trade.getAmount());
        DatabaseConnection conn = new DatabaseConnection();
        conn.open();
        conn.write(trade.getId(), trade.getAmount());
    }

    public BigDecimal getTotalSettled() {
        return totalSettled;
    }
}

Review this code and explain what you would flag, and what core design changes you'd suggest so it's safe to call processBatch from multiple settlement batches running at the same time. Explain your reasoning, you don't need to write out the full corrected file.