Longest Consecutive Sequence Problem Statement

You are provided with an unsorted array/list ARR of N integers. Your task is to determine the length of the longest consecutive sequence present in the array.

Explanation:

The consecutive sequence should be in the form [NUM, NUM + 1, NUM + 2, ..., NUM + L] where NUM is the starting integer of the sequence and L + 1 is the length of the sequence.

Note:

If the array contains any duplicates, consider only one occurrence of the number in the consecutive sequence.

Input:

The first line of input contains a single integer 'T', representing the number of test cases or queries.
The first line of each test case contains integer 'N' denoting the size of the array.
The second line of each test case contains 'N' space-separated integers, the elements of the array.

Output:

For each test case, output a single integer on a new line representing the length of the longest consecutive sequence.

Example:

Input: ARR = [9,5,4,9,10,10,6]
Output: 3
Explanation: The longest consecutive sequence is [4,5,6].

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 10^5
  • -10^9 <= ARR[i] <= 10^9
Note:

You do not need to print the expected output; it's already handled. Just implement the required function.

Performance consideration:

Can you solve this problem with O(N) time complexity and O(N) space complexity?

AnswerBot
1y

The task is to find the length of the longest consecutive sequence in an unsorted array of integers.

  • Sort the array to bring consecutive numbers together

  • Iterate through the sorted array and keep track ...read more

Help your peers!
Select
Add answer anonymously...

Popular interview questions of Software Engineer

A Software Engineer was asked Q1. Bit Set Problem Statement You are provided with a string, termed as DIGIT_PATTER...read more
A Software Engineer was asked Q2. Rectangle Area Calculation Given a list of rectangles, where each rectangle is r...read more
A Software Engineer was asked Q3. Print All Permutations of a String Given an input string STR, generate and print...read more
Grey Orange Software Engineer 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