Find and count target-sum subarrays
TikTokMachine Learning Engineer · Technical ScreenLast updated
Find and count target-sum subarrays evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Given an integer array nums and an integer target, implement: (
- a function that returns true if any non-empty contiguous subarray sums to target; (
- a function that returns the number of contiguous subarrays whose sum equals target; (
- assuming all numbers in nums are non-negative, design an O(n) time, O(
- extra-space method to find one such subarray (return its start and end indices) and explain how to adapt it to count all such subarrays. For (
- and (
- also aim for O(n) time. Discuss trade-offs, edge cases (e.g., zeros, negative numbers, large inputs), and prove correctness.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.