Wiggle Subsequence
Problem Statement
A wiggle is a list of numbers where every step is a rise or a fall, and the steps alternate: up, down, up, down, and so on. A step that is flat (two equal numbers next to each other) breaks the wiggle.
You may drop any numbers from the list, but the rest must keep their order. Return the length of the longest subsequence that is a wiggle.
nums, a list of whole numbers. The list is not sorted.
The length of the longest wiggle subsequence.
The whole list is a wiggle. The steps are +6, −3, +5, −7, +3, which alternate.
One longest wiggle is 1, 17, 10, 13, 10, 16, 8, with steps +16, −7, +3, −3, +6, −8.
Every step goes up, so the most we can keep is two numbers, such as 1 and 9.
- • 1 ≤ nums.length ≤ 1000
- • 0 ≤ nums[i] ≤ 1000
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Take or skip each number, then check each subsequence for the wiggle rule
up[i] and down[i] are the longest wiggles ending at nums[i]; look back at every earlier j
Keep up and down; a rise sets up = down + 1, a fall sets down = up + 1
Each number is either taken or skipped, so there are 2^N subsequences. For each one, check the wiggle rule: every step must be non-zero, and the steps must alternate in sign. Keep the longest one that passes.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Longest Increasing Subsequence translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.