Prepin
Log in

Longest alternating sequence

Approach / Explanation

The "Longest Alternating Subsequence" is a frequently asked question in coding interviews that assesses your grasp of arrays and dynamic programming. To effectively tackle this question, it is essential to first understand the concept of an 'alternating sequence.' An alternating sequence is characterized by each element being either strictly greater or strictly less than its neighboring elements, meaning the sequence alternates between increases and decreases. In this challenge, you will typically be provided with an array (or sequence) of integers. Your objective will be to determine and return the length of the longest alternating subsequence present in that array. Here are some important points to keep in mind when addressing this type of question: Keep track of two counters for both the increasing and decreasing subsequences needed. Iterate through the provided array while keeping a record of the longest subsequence found so far. Compare the current element with the one before it. If the current element is greater, it could potentially be part of a future subsequence, so increment the first counter. If it is smaller, the current element could also be part of a future subsequence, so increment the second counter. The final result will be the greater of the two counters.

Suggested Answer

For instance, if the array provided is [10, 20, 30, 40], the result should be 2. Even though the elements in the array are arranged in increasing order, the longest alternating subsequence is only of length 2. To begin, set up two counters, both initialized to 1, representing the lengths of the longest increasing and decreasing subsequences observed so far. Iterate through the array, comparing each element to the one before it. In this case, since all the numbers are increasing, they do not create an alternating subsequence. Consequently, the longest decreasing subsequence remains at 1, while the longest increasing subsequence grows to 4 by the end of the iteration. The maximum of these counters is 4. However, this count includes all four elements; typically, we should count only those that are strictly increasing or strictly decreasing. Therefore, the longest alternating subsequence is of length 2, which can consist of any two numbers from the sequence.

Alternative Answer

On the other hand, if the array provided is [1, 5, 4], the result should be 3. In this scenario, the numbers alternate between rising and falling, making the longest alternating subsequence the entire sequence itself. To determine this, start by initializing two counters to 1. Iterate through the array and compare each element with the one before it. For the first pair (1, 5), the sequence increases, so you should increase the increasing counter. For the next pair (5, 4), the sequence decreases, prompting you to increment the decreasing counter. The highest value of these counters at the end is 3, which represents the length of the alternating subsequence [1, 5, 4].

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.