Python
All Top Python Interview Questions#48
Implement SQL LAG and LEAD with .shift() in Pandas
EasyFlipkartInterview Question #48
Asked at FlipkartGiven user page visits with timestamps, calculate the time elapsed since the previous visit for each user.
Input Table: df
3 rows preview| user_id | visit_time |
|---|---|
| 101 | 2026-03-01 10:00:00 |
| 101 | 2026-03-01 10:25:00 |
| 102 | 2026-03-01 11:00:00 |
Expected Output Structure3 rows
| user_id | visit_time | elapsed_min |
|---|---|---|
| 101 | 2026-03-01 10:00:00 | NULL |
| 101 | 2026-03-01 10:25:00 | 25 |
| 102 | 2026-03-01 11:00:00 | NULL |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
Python 3.10 (Pandas)
Environment Ready
Starting the real Python + pandas runtime (one-time download)…