Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6601821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:48:10+00:00 2026-05-25T18:48:10+00:00

You are given an Array of numbers and they are unsorted/random order. You are

  • 0

You are given an Array of numbers and they are unsorted/random order. You are supposed to find the longest sequence of consecutive numbers in the array. Note the sequence need not be in sorted order within the array. Here is an example :

Input :

A[] = {10,21,45,22,7,2,67,19,13,45,12,11,18,16,17,100,201,20,101}  

Output is :

{16,17,18,19,20,21,22}  

The solution needs to be of O(n) complexity.

I am told that the solution involves using a hash table and I did come across few implementations that used 2 hash tables. One cannot sort and solve this because sorting would take O(nlgn) which is not what is desired.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:48:10+00:00Added an answer on May 25, 2026 at 6:48 pm

    Here is a solution in Python that uses just a single hash set and doesn’t do any fancy interval merging.

    def destruct_directed_run(num_set, start, direction):
      while start in num_set:
        num_set.remove(start)
        start += direction
      return start
    
    def destruct_single_run(num_set):
      arbitrary_member = iter(num_set).next()
      bottom = destruct_directed_run(num_set, arbitrary_member, -1) 
      top = destruct_directed_run(num_set, arbitrary_member + 1, 1)
      return range(bottom + 1, top)
    
    def max_run(data_set):
      nums = set(data_set)
      best_run = []
      while nums:
        cur_run = destruct_single_run(nums)
        if len(cur_run) > len(best_run):
          best_run = cur_run
      return best_run
    
    def test_max_run(data_set, expected):
      actual = max_run(data_set)
      print data_set, actual, expected, 'Pass' if expected == actual else 'Fail'
    
    print test_max_run([10,21,45,22,7,2,67,19,13,45,12,11,18,16,17,100,201,20,101], range(16, 23))
    print test_max_run([1,2,3], range(1, 4))
    print max_run([1,3,5]), 'any singleton output fine'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an array of real-valued numbers, A[1..n,1..n] , I wish to find the sub-array
Given an unsorted integer array, and without making any assumptions on the numbers in
You are given as input an unsorted array of n distinct numbers, where n
Let's say I have an array of random numbers in no particular order. Let's
This is an open-end interview question. Given an array with 2 numbers are duplicated
I have a string. I need to replace all instances of a given array
Given two floating-point numbers, I'm looking for an efficient way to check if they
I need one liner (or close to it) that verifies that given array of
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an array of objects stored in $my_array , I'd like to extract the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.