groupbyapply

The full problem statement is available to Pro members.

preloaded fixtures

import pandas as pd
import numpy as np

trades = pd.DataFrame({
    "trade_id": range(1, 11),
    "trader": ["ada", "ada", "ada", "bo", "bo", "bo", "cruz", "cruz", "dee", "dee"],
    "symbol": ["AAPL", "AAPL", "TSLA", "AAPL", "NVDA", "NVDA", "TSLA", "TSLA", "AAPL", "MSFT"],
    "side": ["buy", "sell", "buy", "buy", "buy", "sell", "buy", "sell", "buy", "buy"],
    "shares": [10, 5, 4, 3, 2, 1, 6, 6, 20, 5],
    "price": [150.0, 165.0, 210.0, 158.0, 480.0, 610.0, 190.0, 175.0, 170.0, 400.0],
    "traded_on": pd.to_datetime(["2024-01-05", "2024-02-10", "2024-01-20", "2024-01-25", "2024-02-01",
                                  "2024-03-01", "2024-02-05", "2024-02-25", "2024-02-15", "2024-03-02"]),
})

Guest mode · solve history stays on this device; code isn’t saved. Sign in to save future work

Pro problem

“Realized profit and loss”

This is one of the real interview questions in Pro. Try Pro free for 7 days to unlock it and everything below:

  • 293 interview-grade problems across SQL, Python & pandas
  • 104+ real-world SQL problems: SaaS retention, fintech ledgers, supply chain, recursive CTEs
  • 18 company interview packs (Amazon, Meta, Google, Uber and more)
  • AI "explain my mistake" tutor and mock interviews
  • Use QueryLab inside Claude, Cursor & other AI assistants

// built for real interview rounds

Practice the patterns 18 top companies test

329 problems · 36 free forever · 293 unlocked with Pro

Card required. Then $12/month. Cancel before your trial ends and you pay nothing.

or keep practicing free problems →

Secure checkout by Paddle · instant access · cancel anytime from your account