Find longest segment with endpoints > interior
AmazonSoftware Engineer · Online AssessmentLast updated
This question evaluates algorithmic design and array-processing skills, specifically reasoning about subarray endpoint vs interior relationships, range comparisons, and designing solutions with subquadratic time and space complexity.
Given an integer array nums, find the length of the longest contiguous segment [i..j] (j−i+1 ≥ 2) such that both endpoints are strictly greater than every element strictly between them; i.e., max(nums[i+1..j−1]) < min(nums[i], nums[j]). Return only the length. Examples: nums=[1,3,2,4,1] → longest is [3,2,4], length 3; nums=[4,2,6] → [4,2,6], length 3. Brute force is not allowed—design an algorithm faster than O(n^ 2) and provide complexity analysis.
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.