M
mike3
Or some fresh fruit.
What's the point here?
Or some fresh fruit.
mike3 said:Andrew Tomazos said:I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."The assumption being extra points for efficiency.I initially came up with a linear space, linear time solution. With
some prompting and hints from the interviewer we worked our way to a
smaller constant space and linear time algorithm. That being
basically:int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.The interviewer already knew the answer, so I can only assume the test
was based on how much help he had to give me to arrive at it.My question about this is as follows. I, perhaps boldly, claim to
employers to have a "masters-equivalant" experience/knowledge of
computer science and math. Should I have been able to come up with
this solution without prompting or help?
If what you're asking is whether anybody having a master in CS and
maths would have been able to come up with this solution in the
interview time, I guess we can answer that definitely no, otherwise
there would be no point in trying to select candidates with this test.
Obviously, it's because some people (with or without a master diploma,
this really isn't relevant) get or don't get it, that this test is
useful for the recruiter.
Now if you want this kind of jobs, yes you should better be able to
come up with smart solutions to little puzzles like this in
interviews.
You mean he should be able to come up with them without prompting,
right?
You say not a "single" one, so does that mean no course would do it,
or
you'd need multiple ones? After all, courses and books (multiple) can
be used to increase the first 2 areas (knowledge of algorithms and
mathematics).
And how does one develop that? More specifically, the "speed" of
coming up with the solution, not just being able to come up with
one.
What would that be?
mike3 said:<snip>
Oh yes, and I forgot to add: how does one train the "symbolic"
thinking?
Or some fresh fruit.
Andrew Tomazos said:I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:
[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."
So what's the pointy stick for, then?
What's the point here?
Andrew Tomazos said:I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:
[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."
The assumption being extra points for efficiency.
I initially came up with a linear space, linear time solution. With
some prompting and hints from the interviewer we worked our way to a
smaller constant space and linear time algorithm. That being
basically:
int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}
The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.
The interviewer already knew the answer, so I can only assume the test
was based on how much help he had to give me to arrive at it.
My question about this is as follows. I, perhaps boldly, claim to
employers to have a "masters-equivilant" experience/knowledge of
computer science and math. Should I have been able to come up with
this solution without prompting or help?
int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.
What solution? The above exhibits undefined behaviour.
On Nov 28, 4:07 am, Phil Carmody <[email protected]>
wrote:int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}
The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.
What solution? The above exhibits undefined behaviour.
If you spot a bug you might bother saying what it is. I wrote this
quickly and haven't tested it. Are you referring to the assumption
that int is 32 bit, or is there a typo or some other problem?
-Andrew.
I tried this with:
* int p[9] = {4, 4, 4, 5, 6, 7, 5, 5, 4};
* int mode = findmode(p, 9);
and it returns 5.
The bit-counting method correctly returns 4.
It looks to me like you missed Phase 1 from the Fischer&Salzberg
algorithm, namely
arrange the elements so that no two adjacent elements are the same.http://www.cs.yale.edu/publications/techreports/tr252.pdf- Hide quoted text -
- Show quoted text -
I think you missed the definition of the problem in this thread. It
is one of the givens of the problem that the array has a strict
"majority" value that appears in over half ot its entries.
You go on to claim that the code quoted above gives an answer of 5 but
that the "bit-counting method" returns 4. What you fail to realize is
that the code above _is_ the bit-counting method
and that it returns 4, not 5.
I actually ran the code. Did you? Yes
In summary, in spite of your accusation of ill-defined behavior, in
spite of your providing the algorithm with non-compliant input and in
spite of your claim of incorrect output, the fact of the matter is
that the algorithm got what you consider to be the right answer.
I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:
[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."
The assumption being extra points for efficiency.
I initially came up with a linear space, linear time solution. With
some prompting and hints from the interviewer we worked our way to a
smaller constant space and linear time algorithm. That being
basically:
int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}
The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.
The interviewer already knew the answer, so I can only assume the test
was based on how much help he had to give me to arrive at it.
My question about this is as follows. I, perhaps boldly, claim to
employers to have a "masters-equivilant" experience/knowledge of
computer science and math. Should I have been able to come up with
this solution without prompting or help?
Would you expect someone with a CompSci Masters or PhD from some major
ivy league university to be able to come up with this solution without
help?
If so in what course or from which book would you expect to learn the
required knowledge to come up with the above solution?
Is the skill to be able to come up with such an algorithm something
that is learned by studying lots of algorithms and being able to mix
and match the "tricks"? If so, what is a similar commonly known
algorithm(s) on which the above solution could have been based?
Or, is the ability to invent such a solution simply a matter of IQ?
Some people have the talent to solve the puzzle, see the pattern and
come up with the solution - and some just don't?
I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."The assumption being extra points for efficiency.I initially came up with a linear space, linear time solution. With
some prompting and hints from the interviewer we worked our way to a
smaller constant space and linear time algorithm. That being
basically:int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.The interviewer already knew the answer, so I can only assume the test
was based on how much help he had to give me to arrive at it.My question about this is as follows. I, perhaps boldly, claim to
employers to have a "masters-equivilant" experience/knowledge of
computer science and math. Should I have been able to come up with
this solution without prompting or help?Would you expect someone with a CompSci Masters or PhD from some major
ivy league university to be able to come up with this solution without
help?If so in what course or from which book would you expect to learn the
required knowledge to come up with the above solution?Is the skill to be able to come up with such an algorithm something
that is learned by studying lots of algorithms and being able to mix
and match the "tricks"? If so, what is a similar commonly known
algorithm(s) on which the above solution could have been based?Or, is the ability to invent such a solution simply a matter of IQ?
Some people have the talent to solve the puzzle, see the pattern and
come up with the solution - and some just don't?
How badly would the following simple algorithm work:
Look at the first column. Determine which digit occurs more - 0 or 1 -
among the 1,000,000 integers, and mark those integers, that don't have
this digit, as "R" (for "rejects")
I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:
[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."
The assumption being extra points for efficiency.
I initially came up with a linear space, linear time solution. With
some prompting and hints from the interviewer we worked our way to a
smaller constant space and linear time algorithm. That being
basically:
int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}
The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.
The interviewer already knew the answer, so I can only assume the test
was based on how much help he had to give me to arrive at it.
My question about this is as follows. I, perhaps boldly, claim to
employers to have a "masters-equivilant" experience/knowledge of
computer science and math. Should I have been able to come up with
this solution without prompting or help?
Would you expect someone with a CompSci Masters or PhD from some major
ivy league university to be able to come up with this solution without
help?
If so in what course or from which book would you expect to learn the
required knowledge to come up with the above solution?
Is the skill to be able to come up with such an algorithm something
that is learned by studying lots of algorithms and being able to mix
and match the "tricks"? If so, what is a similar commonly known
algorithm(s) on which the above solution could have been based?
Or, is the ability to invent such a solution simply a matter of IQ?
Some people have the talent to solve the puzzle, see the pattern and
come up with the solution - and some just don't?
How badly would the following simple algorithm work:Look at the first column. Determine which digit occurs more - 0 or 1 -
among the 1,000,000 integers, and mark those integers, that don't have
this digit, as "R" (for "rejects")
This requires linear O(n) space to store these reject marks. The two
previously discussed algorithms only require constant O(1) space.
I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:
[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."
The assumption being extra points for efficiency.
I initially came up with a linear space, linear time solution. With
some prompting and hints from the interviewer we worked our way to a
smaller constant space and linear time algorithm. That being
basically:
int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}
The idea here is to count the frequency of each of the 32 bits in the
array separately, knowing that these bit counts will be dominated by
the mode.
The interviewer already knew the answer, so I can only assume the test
was based on how much help he had to give me to arrive at it.
My question about this is as follows. I, perhaps boldly, claim to
employers to have a "masters-equivilant" experience/knowledge of
computer science and math. Should I have been able to come up with
this solution without prompting or help?
Would you expect someone with a CompSci Masters or PhD from some major
ivy league university to be able to come up with this solution without
help?
If so in what course or from which book would you expect to learn the
required knowledge to come up with the above solution?
Is the skill to be able to come up with such an algorithm something
that is learned by studying lots of algorithms and being able to mix
and match the "tricks"? If so, what is a similar commonly known
algorithm(s) on which the above solution could have been based?
Or, is the ability to invent such a solution simply a matter of IQ?
Some people have the talent to solve the puzzle, see the pattern and
come up with the solution - and some just don't?
How badly would the following simple algorithm work:
Look at the first column. Determine which digit occurs more - 0 or 1 -
among the 1,000,000 integers, and mark those integers, that don't have
this digit, as "R" (for "rejects")
This requires linear O(n) space to store these reject marks. The two
previously discussed algorithms only require constant O(1) space.
Do you mean this:
int findmode(int* p, int n)
{
int count[32];
for(int i = 0; i < 32; i++)
count = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < 32; j++)
if (p & (1 << j)) // if bit j is on
count[j]++;
else
count[j]--;
int x = 0;
for (int i = 0; i < 32; i++)
if (count > 0)
x = x | (1 << i);
return x;
}
Doesn't count[j] require O(log n) space?
But of course O(log n) is still better than O(n)
Andrew Tomazos said:I was posed the following question in a technical interview for a
Software Engineering position by a major multinational NASDAQ company:
[Paraphrasing] "You are given an array of 1,000,000 32-bit integers.
One int value x occurs 500,001 times or more in the array. Specify an
algorithm to determine x."
I missed your original post, but dug up the following that might be of
interest:
http://www.cs.utexas.edu/users/moore/best-ideas/mjrty/index.html
Probably I miss something, but this algorithm gives D as the majority
element in the sequence AAAABCBCD. ???
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.