Magicsheet logo

Viewers Turned Streamers

Hard
84.3%
Updated 6/1/2025

Asked by 2 Companies

Topics

Viewers Turned Streamers

What is this problem about?

The "Viewers Turned Streamers" interview question is a database/SQL problem. You are given a table of sessions (viewer/streamer IDs, session types, and timestamps). You need to find the users whose first ever session was as a "viewer" and who later had a session as a "streamer." You then return the count of their streaming sessions, sorted by count in descending order.

Why is this asked in interviews?

PhonePe and Google use the "Viewers Turned Streamers" coding problem to test a candidate's SQL skills, specifically their ability to use "Window Functions" and subqueries. It assesses how you handle user lifecycle analysis and temporal data in a relational database.

Algorithmic pattern used

The primary pattern is the "Database interview pattern" using RANK() or ROW_NUMBER(). First, you identify each user's first session using a window function partitioned by user_id and ordered by timestamp. Filter for those whose first session type is 'viewer'. Then, join this list back to the original table to count their sessions where the type is 'streamer'.

Example explanation

Sessions:

  • User 1: 10:00 (Viewer), 11:00 (Streamer), 12:00 (Streamer)
  • User 2: 09:00 (Streamer), 10:00 (Viewer)
  1. Find first sessions:
    • User 1 first: Viewer (Keep).
    • User 2 first: Streamer (Discard).
  2. For User 1, count streaming sessions after the first:
    • 11:00 (Streamer), 12:00 (Streamer). Count = 2.
  3. Result: User 1 with count 2.

Common mistakes candidates make

A frequent mistake is not correctly identifying the "first" session and instead just checking if a user has both types, which is incorrect. Another error is forgetting to filter for sessions that happened after they became a streamer, or not sorting the final output as requested. Candidates also often miss users who might have multiple viewer sessions before their first streamer session.

Interview preparation tip

For the "Viewers Turned Streamers" coding problem, familiarize yourself with Common Table Expressions (CTEs). Using CTEs to break down the problem into "Find First Session," "Filter Users," and "Count Stream Sessions" makes your SQL query much more maintainable and easier to explain.

Similar Questions