Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j].
Return true if there is a 132 pattern in nums, otherwise, return false.
Example 1:
Input: nums = [1,2,3,4] Output: false Explanation: There is no 132 pattern in the sequence.
Example 2:
Input: nums = [3,1,4,2] Output: true Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
Example 3:
Input: nums = [-1,3,2,0] Output: true Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
Constraints:
n == nums.length1 <= n <= 2 * 105-109 <= nums[i] <= 109Problem summary: Given an array of n integers nums, a 132 pattern is a subsequence of three integers nums[i], nums[j] and nums[k] such that i < j < k and nums[i] < nums[k] < nums[j]. Return true if there is a 132 pattern in nums, otherwise, return false.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Binary Search · Stack · Segment Tree
[1,2,3,4]
[3,1,4,2]
[-1,3,2,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #456: 132 Pattern
class Solution {
public boolean find132pattern(int[] nums) {
int vk = -(1 << 30);
Deque<Integer> stk = new ArrayDeque<>();
for (int i = nums.length - 1; i >= 0; --i) {
if (nums[i] < vk) {
return true;
}
while (!stk.isEmpty() && stk.peek() < nums[i]) {
vk = stk.pop();
}
stk.push(nums[i]);
}
return false;
}
}
// Accepted solution for LeetCode #456: 132 Pattern
func find132pattern(nums []int) bool {
vk := -(1 << 30)
stk := []int{}
for i := len(nums) - 1; i >= 0; i-- {
if nums[i] < vk {
return true
}
for len(stk) > 0 && stk[len(stk)-1] < nums[i] {
vk = stk[len(stk)-1]
stk = stk[:len(stk)-1]
}
stk = append(stk, nums[i])
}
return false
}
# Accepted solution for LeetCode #456: 132 Pattern
class Solution:
def find132pattern(self, nums: List[int]) -> bool:
vk = -inf
stk = []
for x in nums[::-1]:
if x < vk:
return True
while stk and stk[-1] < x:
vk = stk.pop()
stk.append(x)
return False
// Accepted solution for LeetCode #456: 132 Pattern
impl Solution {
pub fn find132pattern(nums: Vec<i32>) -> bool {
let n = nums.len();
let mut vk = i32::MIN;
let mut stk = vec![];
for i in (0..n).rev() {
if nums[i] < vk {
return true;
}
while !stk.is_empty() && stk.last().unwrap() < &nums[i] {
vk = stk.pop().unwrap();
}
stk.push(nums[i]);
}
false
}
}
// Accepted solution for LeetCode #456: 132 Pattern
function find132pattern(nums: number[]): boolean {
let vk = -Infinity;
const stk: number[] = [];
for (let i = nums.length - 1; i >= 0; --i) {
if (nums[i] < vk) {
return true;
}
while (stk.length && stk[stk.length - 1] < nums[i]) {
vk = stk.pop()!;
}
stk.push(nums[i]);
}
return false;
}
Use this to step through a reusable interview workflow for this problem.
Check every element from left to right until we find the target or exhaust the array. Each comparison is O(1), and we may visit all n elements, giving O(n). No extra space needed.
Each comparison eliminates half the remaining search space. After k comparisons, the space is n/2ᵏ. We stop when the space is 1, so k = log₂ n. No extra memory needed — just two pointers (lo, hi).
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.