# Value Count

> How many of each? Count them.

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

Domain: Python · Difficulty: easy · Seniority: L3

## Problem

Given a list and a target value, return the count of list elements equal to target.

## Worked solution and explanation

### Why this problem exists in real interviews

This tests the most basic **counting loop**. It probes whether a candidate can write a clean linear scan with an equality check, the simplest possible algorithm.

---

### Break down the requirements

#### Step 1: Initialize a counter

Start at 0.

#### Step 2: Iterate and count matches

Increment for each element equal to the target.

---

### The solution

**Linear scan with equality check**

```python
def value_count(items: list, target) -> int:
    count = 0
    for item in items:
        if item == target:
            count += 1
    return count
```

> **Time and Space Complexity**
>
> **Time:** O(n) for a single pass.
> 
> **Space:** O(1). One integer counter.

> **Interviewers Watch For**
>
> Clean, minimal code. Even trivial problems are evaluated on code clarity and variable naming.

> **Common Pitfall**
>
> Using `is` instead of `==` for comparison. `is` checks identity (same object), not equality. For integers outside the cached range [-5, 256], `is` can return False even when values are equal.

---

## Common follow-up questions

- What if the list was sorted? _(Tests binary search for O(log n) count via finding first and last occurrence.)_
- What if you needed approximate matching for floats? _(Tests using `abs(item - target) < epsilon`.)_
- How does this compare to the built-in list.count()? _(Tests knowing they are functionally identical; the manual version is for demonstrating fundamentals.)_

## Related

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