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

  • Home
  • SEARCH
  • 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 6151469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:46:23+00:00 2026-05-23T19:46:23+00:00

I came across this post , which reports the following interview question: Given two

  • 0

I came across this post, which reports the following interview question:

Given two arrays of numbers, find if each of the two arrays have the
same set of integers ? Suggest an algo which can run faster than NlogN
without extra space?

The best that I can think of is the following:

  1. (a) sort each array, and then (b) have two pointers moving along the two arrays and check if you find different values … but step (a) has already NlogN complexity 🙁

  2. (a) scan shortest array and put values into a map, and then (b) scan second array and check if you find a value that is not in the map … here we have linear complexity, but we I use extra space

… so, I can’t think of a solution for this question.

Ideas?


Thank you for all the answers. I feel many of them are right, but I decided to choose ruslik’s one, because it gives an interesting option that I did not think about.

  • 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-23T19:46:24+00:00Added an answer on May 23, 2026 at 7:46 pm

    You can try a probabilistic approach by choosing a commutative function for accumulation (eg, addition or XOR) and a parametrized hash function.

    unsigned addition(unsigned a, unsigned b);
    unsigned hash(int n, int h_type);
    
    unsigned hash_set(int* a, int num, int h_type){
        unsigned rez = 0;
        for (int i = 0; i < num; i++)
            rez = addition(rez, hash(a[i], h_type));
        return rez;
    };
    

    In this way the number of tries before you decide that the probability of false positive will be below a certain treshold will not depend on the number of elements, so it will be linear.

    EDIT: In general case the probability of sets being the same is very small, so this O(n) check with several hash functions can be used for prefiltering: to decide as fast as possible if they are surely different or if there is a probability of them being equivalent, and if a slow deterministic method should be used. The final average complexity will be O(n), but worst case scenario will have the complexity of the determenistic method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well I was reading this post and then I came across a code which
I tried to do some research on this and came across the following question
Kicking around some small structures while answering this post , I came across the
I came across this post and found belisarius' answer interesting. Wondering whether he someone
So I came across this post about accessing CRM data from an external application.
Came across this error today. Wondering if anyone can tell me what it means:
I came across this article written by Andrei Alexandrescu and Petru Marginean many years
I came across this recently, up until now I have been happily overriding the
I came across this class while reading a C# book and have some questions.
I came across this snippet of code on MSDN: entityBuilder.Metadata = @res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl;

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.