Longest Window With Equal Clicks and Skips in an Impression Log

Last updated: November 29, 2026

Quick Overview

Given a binary array where 1 means an ad impression was clicked and 0 means it was skipped, return the length of the longest contiguous window with the same number of clicks and skips.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a binary array where 1 means an ad impression was clicked and 0 means it was skipped, return the length of the longest contiguous window with the same number of clicks and skips.

A Google technical screen problem framed around finding balanced stretches in Google Ads impression logs for calibration analysis. The interviewer wants the transform-to-plus-minus-one trick plus a hash map of first-seen prefix sums, and expects you to explain why the first occurrence gives the longest window.

What the Interviewer Expects
  • Maps 0 to -1 so balance means a zero-sum window
  • Uses a prefix sum with a first-occurrence index map
  • Seeds the map with sum 0 at index -1
  • Explains why storing the earliest index maximizes length
  • Achieves O(n) time
Key Topics to Cover
Prefix Sums
Hash Map
Zero-Sum Subarray
Arrays
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
  • Return the window itself, not just its length.
  • What if you need the longest window where clicks exceed skips?
  • How would you handle three outcome types (click, skip, hide) with equal counts?
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 0 and 1 values events.
  • Output: maximum length of a contiguous subarray with equal numbers of 0 and 1; 0 if none.
  • n up to around 10^5, so quadratic scanning is too slow....
High-Level Design

Replace each 0 with -1. A window is balanced exactly when its sum is 0. Let P[i] be the running sum after i elements. The window from i to j-1 sums to P[j] - P[i], so it is balanced when `P[j] == ...


Submit Your Answer
Markdown supported