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 8488573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:34:39+00:00 2026-06-10T21:34:39+00:00

I had an interview today, and they gave me: List A has: f google

  • 0

I had an interview today, and they gave me:

List A has:

f  
google   
gfk  
fat  
...

List B has:

hgt    
google  
koko  
fat  
ffta  
...

They asked me to merge these two list in one sorted List C.

What I said:

I added List B to List A, then I created a Set from List A, then create a List from the Set. The interviewer told me the lists are big, and this method will not be good for performance, he said it will be a nlog(n).

What would be a better approach to this problem?

  • 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-06-10T21:34:41+00:00Added an answer on June 10, 2026 at 9:34 pm

    Well your method would require O(3N) additional space (the concatenated List, the Set and the result List), which is its main inefficiency.

    I would sort ListA and ListB with whatever sorting algorithm you choose (QuickSort is in-place requiring O(1) space; I believe Java’s default sort strategy is MergeSort which typically requires O(N) additional space), then use a MergeSort-like algorithm to examine the “current” index of ListA to the current index of ListB, insert the element that should come first into ListC, and increment that list’s “current” index count. Still NlogN but you avoid multiple rounds of converting from collection to collection; this strategy only uses O(N) additional space (for ListC; along the way you’ll need N/2 space if you MergeSort the source lists).

    IMO the lower bound for an algorithm to do what the interviewer wanted would be O(NlogN). While the best solution would have less additional space and be more efficient within that growth model, you simply can’t sort two unsorted lists of strings in less than NlogN time.

    EDIT: Java’s not my forte (I’m a SeeSharper by trade), but the code would probably look something like:

    Collections.sort(listA);
    Collections.sort(listB);
    
    ListIterator<String> aIter = listA.listIterator();
    ListIterator<String> bIter = listB.listIterator();
    List<String> listC = new List<String>();
    
    while(aIter.hasNext() || bIter.hasNext())
    {
       if(!bIter.hasNext())
          listC.add(aIter.next());
       else if(!aIter.hasNext())
          listC.add(bIter.next());
       else
       {
          //kinda smells from a C# background to mix the List and its Iterator, 
          //but this avoids "backtracking" the Iterators when their value isn't selected.
          String a = listA[aIter.nextIndex()];
          String b = listB[bIter.nextIndex()];
    
          if(a==b) 
          {
             listC.add(aIter.next());
             listC.add(bIter.next());
          }
          else if(a.CompareTo(b) < 0)
             listC.add(aIter.next());
          else
             listC.add(bIter.next());          
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I just had a job interview and they gave me a list of
Today I had an interview there they asked me can we include .c file
I heard this today during interview for java developer. I had to list some
This task has already been asked/answered, but I recently had a job interview that
I had an interview today and the person asked me this question: How do
I had an interview today and was asked this question! code the MS Paint
Today I had an interview on which I asked candidate quite usual and basic
In an interview they had asked an Question like this there are three overloading
Today I had an interview with a gentleman who asked me to determine how
I was asked in an interview to find if the single linked list has

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.