Python
#48

Implement SQL LAG and LEAD with .shift() in Pandas

EasyFlipkart
Interview Question #48
Asked at Flipkart

Given user page visits with timestamps, calculate the time elapsed since the previous visit for each user.

Input Table: df
3 rows preview
user_idvisit_time
1012026-03-01 10:00:00
1012026-03-01 10:25:00
1022026-03-01 11:00:00
Expected Output Structure3 rows
user_idvisit_timeelapsed_min
1012026-03-01 10:00:00NULL
1012026-03-01 10:25:0025
1022026-03-01 11:00:00NULL
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)…

Chat with us