Minimum Size Subarray Sum

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-05-01

Problem Statement

Design an object-oriented solution to find the smallest contiguous subarray in a list of integers such that the sum of its elements is greater than or equal to a given target value.

The design should focus on clean class decomposition, modularity, and scalability for handling large datasets.


🎯 Objective


Given:


•An array of positive integers
•A target integer S


Find the minimum length of a contiguous subarray such that the sum ≥ S.


If no such subarray exists, return 0.


The design should be modular, testable, and follow SOLID principles.


✅ Functional Requirements


1.The system should accept two inputs:
•An array of positive integers
•A target sum S
2.It should output:
•The length of the smallest contiguous subarray with sum ≥ S
•Optionally, the subarray elements themselves
3.Handle edge cases:
•No subarray meets the condition → return 0
•Single-element array
•Very large array inputs
4.Should allow reuse for different algorithms (e.g., sliding window vs prefix sum).


⚙️ Non-Functional Requirements


•Time complexity: O(n) using the sliding window technique.
•Space complexity: O(1).
•Extensible: Easy to plug in other subarray-based algorithms.
•Reusable: Should fit into a broader analytics or data processing system.
•Testable: Each logical unit should be individually testable.


🧱 Key Classes


1. SubarrayAnalyzer


Handles the main algorithm for finding the minimal subarray length.

1 2class SubarrayAnalyzer { 3 public Result findMinSubarrayLength(int[] nums, int target); 4}


2. Result

Encapsulates the outcome of the computation.

1 2class Result { 3 private int minLength; 4 private List<Integer> subarray; 5}


3. AnalyzerStrategy (Optional)

Interface to allow different algorithmic strategies (sliding window, prefix sum, brute force).

1 2interface AnalyzerStrategy { 3 Result analyze(int[] nums, int target); 4}


4. SlidingWindowStrategy

Implements the efficient O(n) solution.

1 2class SlidingWindowStrategy implements AnalyzerStrategy { 3 public Result analyze(int[] nums, int target); 4}


5. AnalyzerApp

Driver class for taking inputs, invoking analyzer, and printing results.

1 2class AnalyzerApp { 3 public static void main(String[] args); 4}


🔄 Example Flow

Input:

1 2nums = [2, 3, 1, 2, 4, 3], target = 7


Process:

•[2,3,1,2,4,3] → sum 15
•Smallest window with sum ≥ 7 is [4,3] → length 2

Output:

1 2Minimum subarray length: 2 3Subarray: [4, 3]


💾 Possible Extensions

•Support for negative numbers.
•Support for floating-point values.
•Return all subarrays matching the minimum length.
•Integrate into a data analytics pipeline for numeric arrays.
•Add streaming mode for real-time subarray processing.


🧠 Key Design Considerations

•Use a sliding window approach for efficiency.
•Keep computation logic separate from data representation.
•Follow Single Responsibility Principle (SRP) — one class for algorithm logic, another for data handling.
•Maintain extensibility for future subarray-related problems (like maximum sum subarray, fixed-size subarrays, etc.).


Solution

Comments