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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:48:39+00:00 2026-05-11T09:48:39+00:00

This is one of an interview question which I had recently. I would like

  • 0

This is one of an interview question which I had recently. I would like to know others perception of approach for this problem.

Question:

You are given a structure which holds employee details with two elements, int department and string name.

struct Employee {      string Name;     int Dept; } 

You are given details of N Employees, among which N/2 employees have Dept == 0 and N/2 employees have Dept == 1, arranged in some arbitrary order. You need to sort the employee details based on their Dept value and it should be stable i.e., the order of 1s and 0s in the original record should be maintained.

For example, given the following sample data:

 Name         Dept  X1           0 X2           1 X3           0 X4           1 X5           0 

after sorting the result should be:

 Name         Dept  X2           1 X4           1 X1           0 X3           0 X5           0 

The algorithm should be stable and the time complexity should be O(N), with constant space for additional variables (which means sorting should be done in-place).

  • 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. 2026-05-11T09:48:40+00:00Added an answer on May 11, 2026 at 9:48 am

    Okey, here is my approach.

    e.g a[] = { 1,0,0,0,1,1,1,0,0,1};

    Pseudocode:

    1. Have two counters, count1 = 0 and count2 = (n/2)+1
    2. Traverse through the array,

      if(arr[ i ] == 1)  {      arr[ i ] = count1++; } else {      arr[ i ] = count2++  }; 
    3. At the end of the traversal, you have array filled with numbers 0 to n-1 like:

      a[ ] = { 0, 5, 6, 7, 1, 2, 3, 8, 9 4} 
    4. Now the problem comes to sort the above resultant array, this can be done in O(N) as below:

      for(j = 0; j <= 1; j++)   {     for(i = 0; i<n; i++)       {           if(arr[ i ] != i)           {               swap(arr[ i ], arr[ arr[ i ] ]);           }       }   } 

      Note: j loop runs only twice irrespective on ‘n’ and has constant complexity. The order of this whole loop is 2*n = O(n).

    5. After the array is sorted, Again traverse through the array and set elements arr[0] to arr[n/2] to '1' and arr[(n/2)+1] to arr[n] as '0'.

    Space complexity is constant and time complexity is O(step2) + O(step4) + O(step5) = n + 2n +n = 4*n = O(n).

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

Sidebar

Related Questions

Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
I recently had this question in an interview to write test cases to test
I came across this post , which reports the following interview question: Given two
In an interview they had asked an Question like this there are three overloading
This was one of my interview questions which i couldn't answer.... Is it related
I was faced this question while one of recent interview : class1 { virtual
Recently in an interview i was asked a question to write a function which
Question from the one interview. Please explain what does this C++ code mean: void
So, this is a common interview question. There's already a topic up, which I
Recently I had a question on the interview - I was asked to compare

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.