Definition
- Two-level decision – if A >= B and A >= C, A wins; else if B >= C, B wins; else C wins.
- >= not > – using greater-than-or-equal ensures a value is always selected even on ties.
- BigDecimal compareTo –
a.compareTo(b) >= 0means A is at least as large; compares numeric value only. - Chained max –
a.max(b).max(c)is the cleanest production code, branch-free. - Tie policy – on a tie, the earliest listed value wins by >= convention; document this choice.
National Bank of India reviews three competing interest-rate offers for Rajesh's fixed deposit and must present the best. The offers are 5%, 18%, and 11%. The system compares them and picks 18%, the highest.
Understanding the Concepts
What is the problem?
Read three numbers A, B, C and print the largest. For 5, 18, 11 the output is 18.
How do you implement it?
package com.bank.service;
import java.math.BigDecimal;
public class BestOfferSelector {
public static BigDecimal largest(BigDecimal a, BigDecimal b, BigDecimal c) {
return a.max(b).max(c);
}
public static BigDecimal largestExplicit(BigDecimal a, BigDecimal b, BigDecimal c) {
if (a.compareTo(b) >= 0 && a.compareTo(c) >= 0) return a;
if (b.compareTo(c) >= 0) return b;
return c;
}
public static void main(String[] args) {
BigDecimal r1 = new BigDecimal("5.00");
BigDecimal r2 = new BigDecimal("18.00");
BigDecimal r3 = new BigDecimal("11.00");
System.out.println("Best offer: " + largest(r1, r2, r3));
System.out.println("Explicit: " + largestExplicit(r1, r2, r3));
}
}
Best offer: 18.00
Explicit: 18.00
Why use >= instead of strict >?
If all three values are equal, strict > fails every test and no branch fires, leaving no result. Using >= guarantees at least one branch succeeds because the first test passes when A equals B and C.
How does compareTo work for the flowchart?
// a.compareTo(b) >= 0 means a >= b numerically
if (a.compareTo(b) >= 0 && a.compareTo(c) >= 0) {
// A is at least as large as both
return a;
} else if (b.compareTo(c) >= 0) {
// B is at least as large as C
return b;
} else {
return c;
}
How does the Stream approach work?
For a dynamic number of values, use Stream.of with reduce. This handles any count and returns an Optional for safety.
import java.math.BigDecimal;
import java.util.stream.Stream;
BigDecimal best = Stream.of(
new BigDecimal("5.00"),
new BigDecimal("18.00"),
new BigDecimal("11.00"))
.reduce(BigDecimal::max)
.orElse(BigDecimal.ZERO);
System.out.println("Best: " + best); // 18.00
What are edge cases?
- All three equal: the first (A) is returned by the >= convention.
- Two equal and one different: the correct maximum is found.
- Negative values: compareTo handles negative BigDecimal correctly.
- Null input: the method should validate and throw
IllegalArgumentExceptionbefore comparing.
public static BigDecimal largestSafe(BigDecimal a, BigDecimal b, BigDecimal c) {
if (a == null || b == null || c == null) {
throw new IllegalArgumentException("Values must not be null");
}
return a.max(b).max(c);
}
What are common mistakes?
// Wrong: using > instead of >=
if (a.compareTo(b) > 0 && a.compareTo(c) > 0) // fails when a == b == c
// Wrong: using equals (scale-sensitive)
if (a.equals(b)) // 12.0 and 12.00 are not equals!
// Correct: >= with compareTo
if (a.compareTo(b) >= 0 && a.compareTo(c) >= 0)
How does the chained max compare?
| Approach | Code | Branches | Readability |
|---|---|---|---|
| Chained max | a.max(b).max(c) | 0 | very clear |
| Explicit compareTo | nested if-else | 2-3 | mirrors flowchart |
| Stream reduce | Stream.of(a,b,c).reduce(BigDecimal::max) | 0 | functional style |
What should a senior engineer know?
In production, a.max(b).max(c) is preferred. The explicit compareTo form is shown when explaining the flowchart logic. The tie behavior (earliest listed wins with >=) should be documented as a business choice. For an array of N values, use a loop or stream reduce. At National Bank of India, Pankaj documented that when multiple interest-rate offers tied, the earliest-submitted offer was selected so the business rule was deterministic and auditable.
What are common interview follow-up questions?
- How would you find the largest of N numbers?
- What is the difference between BigDecimal compareTo and equals?
- Can you find the largest without using any comparison operator? (Math trick: (a+b+|a-b|)/2)
Visual Overview
┌─────────┐
│ Start │
└────┬────┘
▼
Read A, B, C
│
▼
┌──────────────────────────────┐
│ A >= B AND A >= C ? │
└──┬───────────────────────┬──┘
│ Yes │ No
▼ ▼
Print A ┌──────────┐
│ │ B >= C ? │
│ └─┬──────┬─┘
│ │Yes │No
│ ▼ ▼
│ Print B Print C
│ │ │
└───────┬───────────┴──────┘
▼
┌─────┐
│ End │
└─────┘
Key Takeaway
The largest of three values is found with nested comparisons: first test whether A is at least as large as both B and C, and if so A is the maximum; otherwise compare B against C and take the winner. Using greater-than-or-equal rather than strict greater-than guarantees a value is always chosen even when all three tie.
For money, the comparisons must use BigDecimal compareTo, which compares numeric value and ignores scale. Using equals would be scale-sensitive, and relational operators do not work on objects. In production, a.max(b).max(c) is the cleanest form, doing the comparisons internally.
In a banking project at National Bank of India, three competing interest-rate offers came in for Rajesh's fixed deposit, and the system had to present the best. The BestOfferSelector applied nested compareTo comparisons with >= so ties were handled cleanly. Offers of 5%, 18%, and 11% resolved to 18%, selected deterministically.
Remember one sentence: use >= (not >) so ties always produce a result, and compare money with compareTo (not equals) because equals is scale-sensitive.
---