
Best and worst day per asset
hardBest and worst day per asset
Robinhood Pandas Interview Question
Robinhood writes a short note on each asset it lists, and the two dates readers actually want are the best day and the worst day: the sharpest one day gain and the sharpest one day fall. Both are measured against the asset's own previous close.
Using the prices DataFrame, find each symbol, the date of its sharpest one day gain as best_day, that gain as best_pct, the date of its sharpest one day fall as worst_day, and that fall as worst_pct. Give both percentages rounded to 2 decimal places. Assign the answer to result.
Asked of
- Data Analyst
- Business Analyst
- BI Analyst
- Product Analyst
- Analytics Engineer
- Data Scientist
pricesDataFrame24 rows
| Column Name | Type |
|---|---|
| symbol | str |
| priced_on | str |
| close_usd | float64 |
pricesExample Input
| symbol | priced_on | close_usd |
|---|---|---|
| BTC | 2024-05-01 | 58000 |
| BTC | 2024-05-02 | 59200 |
| BTC | 2024-05-03 | 60500 |
| BTC | 2024-05-04 | 61100 |
| BTC | 2024-05-05 | 59800 |
| BTC | 2024-05-06 | 58400 |
| ETH | 2024-05-01 | 2900 |
| ETH | 2024-05-02 | 2860 |
| ETH | 2024-05-03 | 2945 |
| ETH | 2024-05-04 | 3010 |
| ETH | 2024-05-05 | 3080 |
| ETH | 2024-05-06 | 2990 |
Example Output
| symbol | best_day | best_pct | worst_day | worst_pct |
|---|---|---|---|---|
| BTC | 2024-05-03 | 2.2 | 2024-05-06 | -2.34 |
| ETH | 2024-05-03 | 2.97 | 2024-05-06 | -2.92 |
Explanation
BTC rose 2.2 percent on May 3, its sharpest gain in the example, and fell 2.34 percent on May 6. The first day of each asset has nothing before it to measure against, so it can be neither the best nor the worst.
The example above is a small slice of the data. Your code runs against the full DataFrames.
Company
Robinhood
Difficulty
hard
Topic
window functions
Language
Pandas
Your code
Loading Python in the background. You can start writing.