# The One-Way Street

> Monotonic time-series. Direction only.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list of numbers, return True if the sequence is monotonic (entirely non-decreasing OR entirely non-increasing). Equal consecutive values are allowed. An empty or single-element list returns True.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests the ability to check **monotonicity** of a sequence, a key data quality validation. It reveals whether candidates can handle the nuance that a sequence can be monotonically increasing or decreasing, and that both directions should pass.

---

### Break down the requirements

#### Step 1: Track whether the sequence could be non-decreasing

Scan consecutive pairs. If any pair decreases, the sequence is not non-decreasing.

#### Step 2: Track whether the sequence could be non-increasing

Similarly, if any pair increases, the sequence is not non-increasing.

#### Step 3: Return True if either direction holds

The sequence is monotonic if it is entirely non-decreasing or entirely non-increasing.

---

### The solution

**Two-flag scan for monotonicity**

```python
def is_monotonic(values):
    increasing = True
    decreasing = True
    for i in range(1, len(values)):
        if values[i] > values[i - 1]:
            decreasing = False
        if values[i] < values[i - 1]:
            increasing = False
    return increasing or decreasing
```

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

> **Interviewers Watch For**
>
> Do you handle equal consecutive values correctly? Equal values satisfy both non-decreasing and non-increasing, so they should not flip either flag to False.

> **Common Pitfall**
>
> Only checking for strictly increasing or strictly decreasing. A sequence like `[1, 1, 2, 3]` is monotonically non-decreasing even though it has equal consecutive elements.

---

## Common follow-up questions

- What if you needed to distinguish between strictly and non-strictly monotonic? _(Tests adding a check for equal elements.)_
- What if the sequence is empty or has one element? _(Tests that both are trivially monotonic.)_
- How would you find the longest monotonic subsequence? _(Tests a different algorithm: track the length of the current run and the overall best.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/the_one_way_street)
- [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.