GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 03-Apr-2005, 22:38
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Question

Sorting Array Logic ?


Hi,

I would like to get variety of solutions for this problem:

Let us suppose there are several elements in the array consisting of 1 s and 0 s in any different order. The problem is to sort the 1s to one side and 0s to another side, without violating the precondition that one can traverse the array only once (Only Single pass allowed).


-- Karthikeyan
INDIA
  #2  
Old 04-Apr-2005, 02:41
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I cannot help, because I do not fully understand how the program is supposed to work. Could you give more details about the 1's and 0's?
__________________
-Aaron
  #3  
Old 04-Apr-2005, 03:22
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Quote:
Originally Posted by aaroncohn
I cannot help, because I do not fully understand how the program is supposed to work. Could you give more details about the 1's and 0's?

I just need the algorithm for this problem :

Let us presume there is an Integer array holding only 1s and 0s. Eg.

10001101000111001110101010001111

Now i need to sort this array as this :

11111111111111111000000000000000

or

00000000000000011111111111111111


But i need to perform this on a single pass. ie. I can traverse through the entire array of elements only once.

Hope the problem is clear now.

--- Karthikeyan
  #4  
Old 04-Apr-2005, 06:26
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by karthikeyansen
I just need the algorithm for this problem :

Let us presume there is an Integer array holding only 1s and 0s. Eg.

10001101000111001110101010001111

Now i need to sort this array as this :

11111111111111111000000000000000

or

00000000000000011111111111111111


But i need to perform this on a single pass. ie. I can traverse through the entire array of elements only once.

Hope the problem is clear now.

--- Karthikeyan


What's not clear is: is this a contest or a homework assignment?

Regards,

Dave
  #5  
Old 04-Apr-2005, 06:33
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Exclamation

Question from an Interview


Quote:
Originally Posted by davekw7x
What's not clear is: is this a contest or a homework assignment?

Regards,

Dave

This was a typical Interview question asked by a top notch IT company.

Regards,
Karthikeyan
  #6  
Old 04-Apr-2005, 06:47
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by karthikeyansen
This was a typical Interview question asked by a top notch IT company.

Regards,
Karthikeyan

Then I have to ask a couple of questions:

1. By "traverse the array once" do you mean that a single counter is allowed to go from one end of the array to the other, so that each element is accessed only once? Or can we have two counters in a single loop, and one of the counters can be used read all of the elements and write some and the other can write some?

2. Is it required that the array be sorted "in place"? That is, are we required to have only the given array, which will hold the sorted values, or can we have another array to hold the sorted values, whereas the original array remains unchanged?

There are a couple of "easy" ways that I can think of if the answer to either is "no".

Regards,

Dave
  #7  
Old 04-Apr-2005, 07:04
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road

Clarification for your questions


Quote:
Originally Posted by davekw7x
Then I have to ask a couple of questions:

1. By "traverse the array once" do you mean that a single counter is allowed to go from one end of the array to the other, so that each element is accessed only once? Or can we have two counters in a single loop, and one of the counters can be used read all of the elements and write some and the other can write some?

2. Is it required that the array be sorted "in place"? That is, are we required to have only the given array, which will hold the sorted values, or can we have another array to hold the sorted values, whereas the original array remains unchanged?

There are a couple of "easy" ways that I can think of if the answer to either is "no".

Regards,

Dave
Answers for your question:

1. By "traverse the array once" do you mean that a single counter is allowed to go from one end of the array to the other, so that each element is accessed only once? Or can we have two counters in a single loop, and one of the counters can be used read all of the elements and write some and the other can write some?

You may have any number of counters,but the case is, once if you have accessed that element,you may no more access the same.
A simple case is : If you try counting the total number of 1s or 0s in the array, you have not performed your operation in single pass.

2. Is it required that the array be sorted "in place"? That is, are we required to have only the given array, which will hold the sorted values, or can we have another array to hold the sorted values, whereas the original array remains unchanged?

You are supposed to sort only the given array.That means, you need to sort the original array.

Regards,
Karthikeyan
  #8  
Old 04-Apr-2005, 07:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by karthikeyansen
Answers for your question:

1.
You may have any number of counters,but the case is, once if you have accessed that element,you may no more access the same.


2.
You are supposed to sort only the given array.That means, you need to sort the original array.

Regards,
Karthikeyan

OK. I understand 2. Actually allowing two arrays leads to a very practical generalizationwhere we are leaving the original database intact, but creating a new database for some particular purpose. If it's not allowed, OK. They created the problem; they made the rules.


Then for 1, I think you are saying:

Each element can be read only once and written only once. And, given that is an array, the elements can be accessed only by using an array index or the equivalent pointer arithmetic. Is this correct?

I would like to see a solution that meets this requirement and also requirement number 2.

There are four reasons why I think questions like this are asked:

1. This is kind of a puzzle, with no known practical application, but tests the interviewee's ability to think under fire. Maybe there is some simple way that requires a certain kind of ingenuity on the part of the interviewee. Maybe there is some way that requires knowledge of some rather esoteric features of the programming language. Maybe there is some way that is commonly known among the denizens of "top notch IT" companies. Maybe there is a way, but it requires the interviewee to analyze and ask questions about terminology. (What does "traverse only once" mean.) Maybe the interviewer doesn't know a way to do it and wants to see if you can come up with a way, or to see how long it takes you to admit that you don't see how to do it. Maybe it's a test to see whether prospective interviewees have done web searches of "typical interview questions asked by top-notch IT companies".

2. This is an abstraction of something that is very practical and may or may not be difficult to apply. Knowledge of this requires either very creative thinking on your part, or, perhaps, direct experience, in depth, with the principles implied by the question. You have to know how to answer this question right now in order to be considered for employment. Or, if you don't know the answer, it's a point that will be brought up when you are negotiating your salary.

3. The interviewer has his/her own agenda and interviewing style which is forever unkown and unknowable.

4. If it's not one of the three previous reasons, it's probably something else.

Regards,

Dave
  #9  
Old 04-Apr-2005, 23:15
karthikeyansen karthikeyansen is offline
Awaiting Email Confirmation
 
Join Date: Mar 2005
Location: INDIA
Posts: 22
karthikeyansen is on a distinguished road
Exclamation

Is it a deadlock then??


Quote:
Originally Posted by davekw7x
Each element can be read only once and written only once. And, given that is an array, the elements can be accessed only by using an array index or the equivalent pointer arithmetic. Is this correct?

Karthik >> Yes,Correct. It may be read and written once !!

I would like to see a solution that meets this requirement and also requirement number 2.


Regards,

Dave

If this is the case, there must be a solution for this kind of question?
There are several questions of this kind asked in the interviews.So the logic is the most sort one and not syntax and formats.

I am also thinking on the logic and if i get to know i will let the forum know.


Regards,
Karthikeyan
INDIA
  #10  
Old 04-Apr-2005, 23:32
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by karthikeyansen
[color=Purple]If this is the case, there must be a solution for this kind of question?

There are two principles that I always follow in my journey on Spaceship Earth:

1. I never tell everything that I know.


Regards,

Dave
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Function and Array (w/ reference variables) question brookeville C++ Forum 15 07-Dec-2004 01:11
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52
Sorting a 2D array c++ mike3340 C++ Forum 4 15-Dec-2003 13:35

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.