Longest Alternating Subsequence Problem

Given an array ARR of integers, determine the length of the longest alternating subsequence.

Input:

ARR = {Array elements}

Output:

Length of the longest alternating subsequence

Example:

Input:
ARR = {3, 10, 1, 2, 30}
Output:
4
Explanation:

The longest alternating subsequences could be {3, 10, 1, 30} or {3, 10, 2, 30}.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 5000
  • 1 <= ARR[i] <= 10^5

Time limit: 1 second

Note:

A sequence a1, a2, ..., an is alternating if it satisfies either a1 < a2 > a3 < a4 > a5... or a1 > a2 < a3 > a4 < a5....

AnswerBot
1mo

Find the length of the longest subsequence in an array where elements alternate in value.

  • An alternating subsequence can either be increasing then decreasing or vice versa.

  • Example: For ARR = {3, 10, 1,...read more

Help your peers!
Select
Add answer anonymously...

Josh Technology Group Software Developer Intern interview questions & answers

A Software Developer Intern was asked 3mo agoQ. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 3mo agoQ1. Given a linked list, how do you ensure that it is in decreasing order?
A Software Developer Intern was asked 3mo agoQ2. Given a random node in a BST, how do you check if a given element is part of the...read more
A Software Developer Intern was asked Q3. Merge Two Binary Trees Problem Statement You are given the roots of two binary t...read more
Josh Technology Group Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits