# The Second Summit

> Not the top of the mountain - just below it.

Canonical URL: <https://datadriven.io/problems/the_second_summit>

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list of integers with at least two distinct values, return the second-largest distinct value. Do NOT use sort, sorted, min, or max.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests **tracking two distinct extremes in a single pass** without built-in helpers. By prohibiting sort/sorted/min/max, interviewers force candidates to implement the tracking logic from scratch, verifying fundamental algorithmic understanding.

---

### Break down the requirements

#### Step 1: Initialize first and second to negative infinity

Use `float('-inf')` as a sentinel that any real number will exceed.

#### Step 2: Update on each element

If the current value exceeds `first`, cascade `first` to `second` and update `first`. If it only exceeds `second` and is distinct from `first`, update `second`.

#### Step 3: Return the second largest

After scanning all elements, `second` holds the answer.

---

### The solution

**Two-variable tracking for second distinct max**

```python
def second_largest(nums):
    first = float('-inf')
    second = float('-inf')
    for num in nums:
        if num > first:
            second = first
            first = num
        elif num > second and num != first:
            second = num
    return second
```

> **Time and Space Complexity**
>
> **Time:** O(n) with a single pass.
> 
> **Space:** O(1). Two tracking variables.

> **Interviewers Watch For**
>
> Do you handle the cascade correctly? When updating `first`, the old `first` becomes the new `second`. Missing this step loses the second-largest value.

> **Common Pitfall**
>
> Not checking `num != first` in the elif branch. Without this, a list like `[5, 5, 3]` would set `second = 5` (a duplicate of `first`) instead of `3`.

---

## Common follow-up questions

- What if you needed the third largest? _(Tests extending to three variables with cascading updates.)_
- What if duplicates of the largest should count as the second? _(Tests removing the `num != first` condition.)_
- How would you generalize to the kth largest without sorting? _(Tests the quickselect algorithm for O(n) average-case selection.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_second_summit)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.