← IBM Interview Insights

IBM·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

IBM .NET Engineer interview with an array/subarray problem that looked straightforward but had a few edge cases worth thinking through carefully.

Questions Asked (1)

Q1

Given an array of integers, count how many contiguous subarrays of length 2 or more consist of consecutive decreasing elements where each value is exactly one less than the previous.

Algorithms & Data Structures
Author's notes

My first instinct was to brute force it with nested loops and I started explaining that before catching myself.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem and edge cases, then propose an O(n) single-pass solution that tracks the length of the current strictly decreasing run where each element is exactly one less than the previous. For each run of length L, the number of valid subarrays of length >=2 is L*(L-1)/2, which can be accumulated on the fly.

Pro tip: Mention that you can compute the count in one pass without extra space, and explicitly handle edge cases like empty arrays, single-element arrays, and non-consecutive decreases (e.g., [5,3,1]). This shows attention to detail and efficiency.

1. Clarify the problem

Restate the problem in your own words and ask clarifying questions about input constraints, expected output type, and edge cases (e.g., empty array, single element, duplicates).

2. Identify the pattern

Recognize that valid subarrays are contiguous segments where each adjacent pair satisfies arr[i+1] == arr[i] - 1. The length of such a segment determines the number of valid subarrays.

3. Derive the counting formula

For a decreasing consecutive run of length L, any subarray of length >=2 within it is valid. The number of such subarrays is L*(L-1)/2. Explain why this formula works.

4. Design the algorithm

Use a single pass: initialize run_length = 1 and count = 0. For each i from 1 to n-1, if arr[i] == arr[i-1] - 1, increment run_length; else, add run_length*(run_length-1)/2 to count and reset run_length to 1. After the loop, add the final run's contribution.

5. Analyze complexity and edge cases

State that time complexity is O(n) and space complexity is O(1). Walk through edge cases: empty array returns 0, single element returns 0, no valid runs returns 0, and a full decreasing run returns n*(n-1)/2.

Key Points to Mention

  • Definition of a valid subarray: contiguous, length >=2, each element exactly one less than the previous.
  • Single-pass O(n) time and O(1) space solution using run-length tracking.
  • Formula for counting subarrays within a run: L*(L-1)/2.
  • Handling of edge cases: empty array, single element, non-consecutive decreases.
  • Avoiding double-counting by resetting the run length when the condition breaks.
  • Potential follow-up: if the array is very large, the count may exceed 32-bit integer; use 64-bit integer.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.