PracHub
QuestionsPremiumLearningGuidesInterview PrepNEWCoaches

Quick Overview

This question evaluates proficiency in data manipulation and time-based analytics, covering SQL window functions, timestamp arithmetic, and grouping/ordering semantics within the Data Manipulation (SQL/Python) domain.

  • Medium
  • TikTok
  • Data Manipulation (SQL/Python)
  • Data Scientist

Compute last-to-previous ad impression gaps

Company: TikTok

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: Medium

Interview Round: Technical Screen

Given impressions(ad_id INT, user_id INT, impression_ts TIMESTAMP) with multiple impressions per (user_id, ad_id), write a single SQL query to return, for each (user_id, ad_id) that has at least two impressions, the last impression timestamp, the second‑last impression timestamp, and the time difference between them in seconds as diff_seconds. Break ties by timestamp, using ROW_NUMBER within each (user_id, ad_id) ordered by impression_ts DESC. Assume UTC timestamps. Provide the query in standard SQL using window functions. Use the sample data below (today is 2025‑09‑01): Schema: - impressions(ad_id INT, user_id INT, impression_ts TIMESTAMP) Sample rows: +--------+---------+---------------------+ | ad_id | user_id | impression_ts | +--------+---------+---------------------+ | 101 | 1 | 2025-09-01 11:55:00 | | 101 | 1 | 2025-09-01 12:00:00 | | 101 | 1 | 2025-09-01 12:05:00 | | 101 | 2 | 2025-09-01 13:00:00 | | 101 | 2 | 2025-09-01 13:15:00 | | 102 | 1 | 2025-09-01 14:00:00 | +--------+---------+---------------------+ Expected outputs include (101,1) diff_seconds=300 and (101,2) diff_seconds=900; exclude (102,1) because it has only one impression.

Quick Answer: This question evaluates proficiency in data manipulation and time-based analytics, covering SQL window functions, timestamp arithmetic, and grouping/ordering semantics within the Data Manipulation (SQL/Python) domain.

Last updated: Mar 29, 2026

Loading coding console...

PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Find high-value crypto users and top-CTR product - TikTok (easy)
  • Write monthly customer and sales SQL queries - TikTok (easy)
  • Find top-paid employee per department - TikTok (easy)
  • Count buggy vs non-buggy by employer - TikTok (Medium)
  • Select max-discount product per category - TikTok (Medium)