Second Largest Element Array Java | Fresher Interview
Fast recall
Quick revision
Core answer
largest and second during one scan. A new maximum moves the old maximum into second place; another value updates second only when it lies strictly between them. This is O(n) time and O(1) space. State whether duplicates count, and return an explicit no-answer result when the array has fewer than two distinct values.Whether you do it in one pass with two trackers instead of sorting, handling duplicates
Sorting (O(n log n)) when O(n) works, or returning a duplicate of the max as the second largest
State whether second means distinct and return an explicit no-answer result when fewer than two distinct values exist
Interview-ready guide
Interview answer
I would first clarify whether the question means the second distinct largest value.
For that version, I can avoid sorting and scan once with two trackers. When a number is greater than largest, the old largest moves to second and the new value becomes largest. Otherwise, a value updates second only when it is below largest but above the current second.
For example, in [5, 1, 5, 3, 4], the repeated 5 is ignored, 3 replaces 1, and 4 becomes the answer. This gives O(n) time and O(1) extra space instead of O(n log n) sorting. MIN_VALUEfor[7, 7]`, because that value can also be a valid array element.
empty()` when the second tracker was never set. That makes the no-answer case explicit and still handles every possible integer input.
Second Largest Element Array Java | Fresher Interview
Direct Answer
For the second distinct largest value, keep largest and second during one scan. A new maximum moves the old maximum into second place; another value updates second only when it lies strictly between them. This is O(n) time and O(1) space. State whether duplicates count, and return an explicit no-answer result when the array has fewer than two distinct values.
Deep Dive & Architecture
Keep largest and second as the two greatest distinct values seen so far. When a value is greater than largest, move the old largest into second place and replace it. Otherwise, update second only when the value is smaller than largest but greater than the current second.
Worked example: [5, 1, 5, 3, 4]
After 5, largest is 5. Value 1 becomes second. The next 5 is equal to the largest, so it is ignored when the requirement is the second distinct largest. Value 3 replaces 1, and value 4 replaces 3. The answer is 4, not 5.
Clarify whether “second largest” means distinct. If duplicates count as separate positions, [5, 5, 4] may return 5; most interview versions ask for the second distinct value. An array with fewer than two distinct values, such as [7, 7], has no answer and should return an optional value or throw a documented exception rather than a misleading sentinel.
One scan gives O(n) time and O(1) extra space. Sorting also works in O(n log n), but it changes the array unless copied and does more work than needed. Using nullable wrappers or explicit flags avoids collision with a real Integer.MIN_VALUE input.
Common Mistakes & Pitfalls
- Sorting unnecessarily: O(n log n) when one O(n) scan works.
- Counting a duplicate maximum: decide whether the result must be distinct.
- Starting at zero: that fails for all-negative arrays.
- Using
Integer.MIN_VALUEas both data and 'missing': the value is legal input, so use a separate presence rule. - Ignoring no-answer input:
[7, 7]has no second distinct value.
Check your understanding
Answer these without looking back at the article.
- How do you write a FizzBuzz program in Java?
- How do you check if a number is prime in Java?
- How do you rotate an array by k positions in Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.