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: user_visits
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 Structure2 rows
user_idvisit_timeelapsed_min
1012026-03-01 10:00:00NULL
1012026-03-01 10:25:0025
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

Click "Run & Test" to execute your Pandas code against the test assertions.

Chat with us