Medium
Meta-styleGoogle-style

Count Contiguous Runs Summing to a Target DSA Interview

Given a sequence of integers (possibly negative) and a target sum, count how many contiguous subarrays sum exactly to the target.

1. Problem Statement

You are given an array of integers and a target sum. Count the number of contiguous subarrays whose elements sum to the target.

2. Live Coding Format

Choose Python, JavaScript, Java, or C++, then solve while explaining your reasoning to the live interviewer.

Validation uses handwritten tests only. The MVP does not compile or run code, and it does not provide AI algorithm completion.

3. Key Focus Areas

  • 1
    Problem clarification and contract
  • 2
    Approach exploration and optimality
  • 3
    Think-aloud communication and pacing
  • 4
    Implementation correctness
  • 5
    Code quality and language fluency
  • 6
    Handwritten tests and dry run
  • 7
    Debugging and self-correction
  • 8
    Complexity analysis
  • 9
    Follow-up performance

4. What Strong Candidates Should Demonstrate

  • Clarify sign, size, and zero-sum assumptions before coding.
  • Explain a brute-force baseline and improve to a prefix-sum with hash map frequency count.
  • Think aloud while implementing readable, correct code.
  • Validate with explicit expected outputs and a top-to-bottom handwritten dry run.
  • Analyze time and space complexity and handle a bounded follow-up modification.

Evaluation Guide

This is an evaluation framework, not a single model answer. Strong designs may make different choices when their assumptions and trade-offs are explicit.

Problem clarification and contract

10%

Senior signals

  • Clarifies whether values can be negative or zero, whether the target can be zero, and what to return when no subarray qualifies.
  • Confirms whether overlapping subarrays with the same bounds should be counted once.

Staff-level signals

  • Asks about input size or whether this runs against a fixed array with repeated target queries.

Common red flags

  • ×Codes immediately while assuming all values are positive without confirming.

Approach exploration and optimality

20%

Senior signals

  • Explains the brute-force nested-sum approach, identifies the repeated summation bottleneck, and improves to a running prefix sum with a hash map of prefix-sum frequencies.
  • Explains why the count of previously seen prefix sums equal to (currentPrefix - target) gives the number of valid subarrays ending at the current index.

Staff-level signals

  • Discusses how the approach would change for a fixed array with many repeated target queries versus a single query.

Common red flags

  • ×Starts implementation without explaining an approach even after one interviewer prompt.
  • ×Proposes sorting the array, which breaks the contiguity requirement.

Think-aloud communication and pacing

10%

Senior signals

  • Explains the plan before coding and narrates the prefix-sum invariant while implementing.
  • Paces the core solution to leave time for validation and the follow-up.

Common red flags

  • ×Writes substantial code silently and cannot explain what the code is doing.

Implementation correctness

25%

Senior signals

  • Implements a correct running prefix sum, initializes the frequency map with prefix sum zero occurring once, and updates the count before or after appropriately.
  • Handles negative values, zeros, and the empty-array case.
  • Provides a convincing correctness argument for the prefix-sum difference technique.

Common red flags

  • ×Forgets to seed the frequency map with a zero prefix sum, causing subarrays starting at index zero to be miscounted.
  • ×Final code contains a material correctness bug that the candidate does not identify.

Code quality and language fluency

15%

Senior signals

  • Produces readable, language-appropriate code with clear variable names for running sum and frequency map.

Common red flags

  • ×Contains off-by-one errors or syntax-level incoherence that prevents credible reasoning.

Handwritten validation, dry run, and debugging

10%

Senior signals

  • Provides explicit inputs and expected outputs before tracing the code from top to bottom.
  • Covers negative values, zeros, target zero, and no-solution cases.
  • Finds and fixes an error independently, then re-validates the affected path.

Common red flags

  • ×Claims the code works without a representative trace.

Complexity analysis and follow-up

10%

Senior signals

  • States linear time and linear auxiliary space and compares them with the quadratic brute-force baseline.

Staff-level signals

  • Discusses repeated-query scenarios or streaming constraints without losing focus.

Common red flags

  • ×Cannot state the complexity of the implemented solution.
  • ×Fails a reasonable follow-up despite having at least five minutes and receiving bounded clarification.

Trade-offs to articulate

  • Nested summation uses constant extra space but quadratic time.
  • Prefix sums with a hash map improve time to linear while using linear auxiliary space.
  • A sliding window only works directly when all values are guaranteed non-negative.

Practice follow-up questions

  1. 1.Coding follow-up: modify the solution to return the length of the longest subarray that sums exactly to the target, instead of the count of all qualifying subarrays.
  2. 2.Verbal extension: how would the approach change if the array were effectively immutable and queried with many different targets?

Want interactive feedback?

Practice this problem in the live coding workspace while the interviewer probes your clarification, algorithm, implementation, complexity, and handwritten tests.

Start Interview

Core Concepts

ArraysHash MapsPrefix SumsComplexity Analysis