Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Start free
LAG and LEAD Around Vehicle Views
00:00
5 left

LAG and LEAD Around Vehicle Views

MediumSQL · PostgreSQL

Problem

Carvana's Product Analytics team wants to understand how shoppers move through vehicle detail pages during completed web sessions. Write a PostgreSQL query using LAG and LEAD to expose the previous and next action surrounding each vehicle view.

Requirements

  1. Include only completed web sessions that started in January 2026.
  2. Return one row for each vehicle_view event, including the previous event type, next event type, and seconds until the next event.
  3. Partition the window calculations by session and order events chronologically, using event_id as a tie-breaker.
  4. Preserve NULL when there is no previous or next event.

Output details

  • Return columns in this order: session_id, vehicle_id, event_ts, previous_event_type, next_event_type, seconds_to_next.
  • Sort results by session_id, then event_ts, then event_id, all ascending.
  • Include sessions starting at or after TIMESTAMP '2026-01-01 00:00:00' and before TIMESTAMP '2026-02-01 00:00:00'.
  • Report seconds_to_next as an integer using PostgreSQL cast semantics for elapsed epoch seconds.

Schema

sessions
ColumnTypeDescription
session_idPKVARCHAR(20)Unique browsing session identifier
user_idINTShopper identifier
platformVARCHAR(20)Product platform used during the session
session_statusVARCHAR(20)Outcome of the browsing session
started_atTIMESTAMPSession start timestamp
vehicle_events
ColumnTypeDescription
event_idPKINTUnique event identifier
session_idVARCHAR(20)Session associated with the event
vehicle_idVARCHAR(20)Carvana vehicle associated with the event
event_typeVARCHAR(30)Action recorded on the product surface
event_tsTIMESTAMPEvent occurrence timestamp
Tablessessionsvehicle_events
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.