Shortest Run of Hours Whose Net Follower Gain Reaches K

Last updated: November 29, 2026

Quick Overview

Given hourly net follower changes for a creator (values may be negative) and a target K, return the length of the shortest contiguous run of hours whose sum is at least K, or -1.

ByteDance
Coding & Algorithms
Software Engineer
ByteDance
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Given hourly net follower changes for a creator (values may be negative) and a target K, return the length of the shortest contiguous run of hours whose sum is at least K, or -1.

Douyin creator analytics highlights the shortest window in which a creator gained a milestone number of followers. Because unfollows make values negative, the usual sliding window breaks, and the interviewer wants to see prefix sums with a monotonic deque and a clear explanation of why both pops are safe.

What the Interviewer Expects
  • Explain why a two-pointer window fails with negatives
  • Use prefix sums to turn the problem into pair search
  • Maintain a deque of indexes with increasing prefix values
  • Pop from the front when a valid window is found, and from the back when dominated
  • Verify against a brute force on random inputs
Key Topics to Cover
monotonic deque
prefix sums
sliding window
negative numbers
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 return the start and end hour too?
  • What if you need the longest window with sum at most K?
  • How would you answer this for many creators streaming in real time?
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

Input: list of integers (positive or negative) and K >= 1. Output: the minimum length of a non-empty subarray with sum >= K, or -1....

High-Level Design

Let P[j] be the prefix sum of the first j values. We want the smallest j - i with P[j] - P[i] >= K. Keep a deque of candidate start indexes with strictly increasing prefix values. For each j: wh...


Submit Your Answer
Markdown supported