Stock Range Finder

A medium Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.

Domain
Python
Difficulty
medium
Seniority
L3

Problem

The market-data team dumps end-of-day OHLCV bars as a list of CSV rows, one string per ticker per trading day in the form Date,Ticker,Open,High,Low,Close,Volume (no header row). A trader wants a quick volatility snapshot for one symbol: the day it had the widest intraday swing (High minus Low, earliest Date on ties) and its average daily Volume over the dump. Implement analyze_ticker(lines, ticker) that returns a dict with keys max_range (float, the largest High-Low value), max_range_date (str, the date of that day), and avg_volume (float, the mean Volume across the symbol's rows). Return an empty dict if the symbol never traded.

Prices move. One stretch had the widest gap.

Example

Input:

lines = ["2024-01-15,AAPL,180.0,185.0,178.0,183.0,50000","2024-01-15,GOOG,140.0,142.0,139.0,141.0,30000","2024-01-16,AAPL,183.0,190.0,182.0,189.0,60000","2024-01-17,AAPL,189.0,191.0,188.0,190.0,40000"], ticker = "AAPL"

Output:

{"max_range":8,"max_range_date":"2024-01-16","avg_volume":50000}

Worked solution and explanation

Why this problem exists in real interviews

Parsing CSV data, filtering by a key, and computing aggregate statistics tests end-to-end data pipeline thinking: string parsing, type conversion, filtering, and aggregation in a single function.


Break down the requirements

Step 1: Parse CSV rows into structured fields

Split each row string by comma and map to Date, Ticker, Open, High, Low, Close, Volume.

Step 2: Filter for the target ticker

Only process rows matching the requested ticker.

Step 3: Find the date with the largest High minus Low range

Track the maximum range and the corresponding date as you iterate.

Step 4: Compute the average volume

Accumulate total volume and count to compute the average. Return an empty dict if no rows matched.


The solution

CSV parsing with filter, range tracking, and average
def analyze_ticker(lines, ticker):
    max_range = 0.0
    max_range_date = ''
    total_volume = 0
    count = 0
    for line in lines:
        parts = line.split(',')
        if parts[1] != ticker:
            continue
        date = parts[0]
        high = float(parts[3])
        low = float(parts[4])
        volume = int(parts[6])
        day_range = high - low
        if day_range > max_range:
            max_range = day_range
            max_range_date = date
        total_volume += volume
        count += 1
    if count == 0:
        return {}
    avg_volume = total_volume / count
    result = {
        'max_range': max_range,
        'max_range_date': max_range_date,
        'avg_volume': avg_volume
    }
    return result

Common follow-up questions