Count Active Subscribers on Each Day From Start and End Dates

Last updated: November 29, 2026

Quick Overview

You are given subscription records as (start_day, end_day) pairs, inclusive, with days numbered 0 to D-1. Return an array with the number of active subscribers on each day.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


You are given subscription records as (start_day, end_day) pairs, inclusive, with days numbered 0 to D-1. Return an array with the number of active subscribers on each day.

This difference-array question appears in the style of Meta online assessments and connects to products like Meta Verified, where finance teams need daily active subscriber counts. The interviewer wants to see you avoid the O(n * D) loop that increments every covered day.

What the Interviewer Expects
  • Notices the naive nested loop is too slow when D and n are large
  • Uses a difference array with +1 at start and -1 after end
  • Computes a prefix sum to recover daily counts
  • Handles inclusive end days correctly by subtracting at end + 1
  • Clips records that extend past the reporting window
Key Topics to Cover
Difference array
Prefix sums
Sweep line
Range updates
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you answer arbitrary range queries for total subscriber-days?
  • What changes if subscriptions arrive and cancel in real time?
  • How would you find the peak day and its count?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Inputs: D, the number of days, and subs, a list of inclusive (start, end) day pairs. Output: list of length D with the active count per day. Records may begin before day 0 or end after D-1, so c...

High-Level Design

Each subscription adds 1 to a contiguous block of days. Record that as two point updates on a difference array: diff[start] += 1 and diff[end + 1] -= 1. A running prefix sum over diff then yield...


Submit Your Answer
Markdown supported