Velocity Reviews > C++ > PLS HELP w/ Interview Question!!

# PLS HELP w/ Interview Question!!

Nobody
Guest
Posts: n/a

 11-18-2006

"Frederick Gotham" <(E-Mail Removed)> wrote in message
news:S8K7h.16143\$(E-Mail Removed)...
> Nobody:
>
>> given an unsorted listed such as:
>>
>> 3,1,3,7,1,2,4,4,3
>>
>> find the FIRST UNIQUE number, in this case 7... and of course the list
>> can be millions long...

>
>
> I've only recently begun reading a book on the C++ Standard Library, so
> I'm
> not very familiar with all the built-in iterator routines and so forth, so
> I can't provide an optimal solution.
>
> However, I could give you a inefficient solution that gets the job done by
> simply putting thought into it:
>
> (This code won't be Grade A because I'm only starting to get the hang of
> iterators and so forth. Expect mistakes.)
>
> #include <cstddef>
>
> template<class Iterator,class T>
> std::size_t QuantOccur(Iterator begin,Iterator const end,T const &obj)
> {
> size_t quant = 0;
>
> while (end!=begin) if (*begin++==obj) ++quant;
>
> return quant;
> }
>
> template<class Iterator>
> Iterator FindFirstUnique(Iterator begin,Iterator const end)
> {
> for (;end!=begin;++begin) if (1==QuantOccur(begin,end,*begin)) break;
>
> return begin;
> }
>
> #include <iostream>
> #include <ostream>
>
> int main()
> {
> int arr[] = {3,1,3,7,1,2,4,4,3};
>
> int i = *FindFirstUnique(arr,arr+sizeof arr/sizeof*arr);
>
> std::cout << i << std::endl;
> }
>
>
> For all I know, there could be a Standard Library facility that does this
> with a single line of code!
>
> --
>
> Frederick Gotham

If I understand the algorithm, you go through the list and get the occurence
count of each item, and stop on the first one where count = 1.

Hmm... Big-O wise.. its something like

for 0 .. n // loop through list
for 0 .. n // get occurence count for outer loop

So the runtime here appears to be n^2. Its much cleaner and simpler then my
original 2*(n^2) algorithm.. Hehe.... no to sound picky here, but I didn't
get the job because my algorithm was apperently not good enough... so I'm
trying to nail this down further then even the AVL tree approach with O(n +
n log n)...

I'm wondering now if building an AVL tree is really n log n... since the
outer n is constant, but the inner n is increasing...

Alf P. Steinbach
Guest
Posts: n/a

 11-18-2006
* Nobody:
> I've been looking for a job for a while now, and have run into this
> interview question twice now... and have stupidly kind of blown it twice...
> (although I've gotten better)... time to finally figure this thing out...
>
> basically the interview question is: given an unsorted listed such as:
>
> 3,1,3,7,1,2,4,4,3
>
> find the FIRST UNIQUE number, in this case 7... and of course the list can
> be millions long...

[snip]
> Any ideas?

Yeah, post in [comp.programming], it's off-topic in clc++, and don't
post questions that have better than 50% chance of being HOMEWORK.

Hints: (1) you can't do it in less than linear time, (2) it's trivial to
do it in linear time with linear memory consumption, (3) with more
stringent conditions it becomes a challenge just to find out whether
it's doable.

Follow-ups to [comp.programming].

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Nobody
Guest
Posts: n/a

 11-18-2006

"Alf P. Steinbach" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>* Nobody:
>> I've been looking for a job for a while now, and have run into this
>> interview question twice now... and have stupidly kind of blown it
>> twice... (although I've gotten better)... time to finally figure this
>> thing out...
>>
>> basically the interview question is: given an unsorted listed such as:
>>
>> 3,1,3,7,1,2,4,4,3
>>
>> find the FIRST UNIQUE number, in this case 7... and of course the list
>> can be millions long...

>
> [snip]
>> Any ideas?

>
> Yeah, post in [comp.programming], it's off-topic in clc++, and don't post
> questions that have better than 50% chance of being HOMEWORK.
>
> Hints: (1) you can't do it in less than linear time, (2) it's trivial to
> do it in linear time with linear memory consumption, (3) with more
> stringent conditions it becomes a challenge just to find out whether it's
> doable.
>
>
>
> Follow-ups to [comp.programming].
>

Thanks, I've taken it there. By the way, I've been out of school for 12
years now .

Gianni Mariani
Guest
Posts: n/a

 11-19-2006
Nobody wrote:
....
> Any ideas?

You could use an "index" sort and this would be O(n) - this only works
if the elements are "indexable" i.e. a limited range otherwise you're
limited to nlogn.

i.e.
using an index sort:

a) create a "tally" vector<int> sized to the max element in the set.
b) scan through the elements incrementing the tally[index] in the vector.
c) scan through the elements a second time and stop on the first one
with tally[index] ==1.

If you can't use an index, you can sort the elements through a double
indirection:

a) create a "flags" vector<pair<bool,iterator>> setting first to true
and second to each consecutive iterator of the element list
b) create a "pointers" initialized to point to each element in flags
c) use a special compare routine that compares two * pair<bool,iterator>
if the (*second) are equal then set the first to false on both
operands.
d) scan through the "flags" vector looking for the first element with
first as "true".

This uses a property of sort algorithms (except for index sorts) that is
tricky to describe but I'll give it a go. While performing any sort on
any list of elements, any element that is duplicated must be compared to
at least one other duplicated element. Hence if you instrument the
compare operator of a sort algorithm, you can eliminate duplicate
elements. The corollary is that removing duplicates from a list of
elements need not be more complex that sorting. It's probably not any
easier than sorting either.

G

Here is an implementation of the second algorithm:

// ======== FirstUniqueDupeEliminator =================================
/**
* Helper for FirstUnique().
*
*/

template <
typename w_iterator
>

class FirstUniqueDupeEliminator
{
public:

typedef
std:air<
bool,
w_iterator
> Element;

std::vector< Element > m_values;
std::vector< Element * > m_values_ptrs;

w_iterator m_result;

void Remove( Element * i_listiter )
{
i_listiter->first = false;
}

bool operator()( Element * i_rhs, Element * i_lhs )
{
// if we're pointing to the same element
// we don't do anything special
if ( i_lhs == i_rhs )
{
return false;
}

if ( *(i_lhs->second) < *(i_rhs->second) )
{
return true;
}

if ( *(i_rhs->second) < *(i_lhs->second) )
{
return false;
}

// oops they're the same eliminate the elements from the list
//
Remove( i_rhs );
Remove( i_lhs );

return false;
}

FirstUniqueDupeEliminator(
const w_iterator & i_begin,
const w_iterator & i_end
)
{
if ( i_begin == i_end )
{
m_result = i_end;
return;
}

w_iterator l_tmp = i_begin;

while ( l_tmp != i_end )
{
m_values.push_back( Element( true, l_tmp ) );

++ l_tmp;
}

m_values_ptrs.resize( m_values.size() );

for ( std::size_t i = 0; i < m_values.size(); ++i )
{
m_values_ptrs[ i ] = & m_values[ i ];
}

std::sort( m_values_ptrs.begin(), m_values_ptrs.end(), *this );

for ( std::size_t i = 0; i < m_values.size(); ++i )
{
if ( m_values[ i ].first )
{
m_result = m_values[ i ].second;
return;
}
}

m_result = i_end;
}

operator w_iterator() const
{
return m_result;
}

};

// ======== FirstUnique ===============================================
/**
* FirstUnique returns the first unique element in a container
*
* @param i_begin The first element in the continer
* @param i_end The last element in the container
*
* @return The iterator to the first element in the
* range that is unique or i_end if no elements are unique
*/

template <
typename w_iterator
>

w_iterator FirstUnique(
const w_iterator & i_begin,
const w_iterator & i_end
) {

return FirstUniqueDupeEliminator<w_iterator>( i_begin, i_end );
}

const int array[] = { 3,1,3,7,1,2,4,4,3 };

int main()
{

const int * l_val = FirstUnique(
&array[0], at::ArrayEnd( array )
);
}

Andrew Koenig
Guest
Posts: n/a

 11-19-2006
"Nobody" <(E-Mail Removed)> wrote in message
news:IiK7h.216\$(E-Mail Removed)...

> Hehe... that was kind of the point of my question. In the interview, I got
> it down to O(3n + n log n), later on at home I got it down to O(n + n log
> n). I'm trying to see if there is a faster way.

If someone told me that an algorithm was O(3n+n log n), I would be wondering
whether that person really understood what big-O notations means, because
O(3n+n log n) means exactly the same as O(n log n).

Andrew Koenig
Guest
Posts: n/a

 11-19-2006
"Wayne Marsh" <(E-Mail Removed)> wrote in message
news:455f6937\$0\$2446\$(E-Mail Removed)...

> Create a vector of bools initialised to false the same size as the list,
> and then run through the list, setting the item in the vector at the
> position of the number encountered to true, and removing ones that are
> already true. Then simply look at the first element of the vector. Bingo.
> One pass, right?

> I bet I've made some glaring error in process or efficiency.

Doesn't the vector need to be as large as the number of possible values for
an element in the original vector? That is, if the original vector elements
are int, and an int on your machine has a range of (-2^31) <= n < (2^31),
then don't you need 2^32 elements in your boolean vector?

Nobody
Guest
Posts: n/a

 11-19-2006

"Andrew Koenig" <(E-Mail Removed)> wrote in message
news:FyN7h.303573\$(E-Mail Removed)...
> "Nobody" <(E-Mail Removed)> wrote in message
> news:IiK7h.216\$(E-Mail Removed)...
>
>> Hehe... that was kind of the point of my question. In the interview, I
>> got it down to O(3n + n log n), later on at home I got it down to O(n + n
>> log n). I'm trying to see if there is a faster way.

>
> If someone told me that an algorithm was O(3n+n log n), I would be
> wondering whether that person really understood what big-O notations
> means, because O(3n+n log n) means exactly the same as O(n log n).
>
>

Yeah, that was another one of my mistakes during the interview ... I also
stumbled on the fact that a quick sort was n log n and not log n... I asked
a friend who is very good at algorithms, and he mistakenly blurted out log n
as well til I pointed it out to him .

Honestly, I didn't really study up on algorithms per say, because I was
going in for a user interface type position and focused on that... you live
and learn I guess...

Nobody
Guest
Posts: n/a

 11-19-2006

"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:455fa4f1\$0\$28188\$(E-Mail Removed)...
> Nobody wrote:
> ...
>> Any ideas?

>
> You could use an "index" sort and this would be O(n) - this only works if
> the elements are "indexable" i.e. a limited range otherwise you're limited
> to nlogn.
>
> i.e.
> using an index sort:
>
> a) create a "tally" vector<int> sized to the max element in the set.
> b) scan through the elements incrementing the tally[index] in the vector.
> c) scan through the elements a second time and stop on the first one
> with tally[index] ==1.
>
> If you can't use an index, you can sort the elements through a double
> indirection:
>
> a) create a "flags" vector<pair<bool,iterator>> setting first to true
> and second to each consecutive iterator of the element list
> b) create a "pointers" initialized to point to each element in flags
> c) use a special compare routine that compares two * pair<bool,iterator>
> if the (*second) are equal then set the first to false on both
> operands.
> d) scan through the "flags" vector looking for the first element with
> first as "true".

Thanks for the response and the code Gianni. I took the thread over to
comp.lang.algorithms as a poster requested. The AVL tree solution is
actually O (n log n) as well, but as per Alf, he pointed out that there is a
trivial O (n) solution.

I was totally stumped til someone mentioned the key word: hash table.

basically the O (n) "trivial" solution was to loop through the list like so:

for (0..n)
{
hash[element[n]] = 1;
else
hash[element[n]] = hash[element[n]] + 1;
}

that'll build a hash table of the occurance count of each item in the array.

then loop through like this to find the first unique element...

for (0..n)
{
if (hash.Find(element[n]) == 1)
FOUND IT
}

that ends up being O(2n) = O(n).

no sorting or deletions needed ...

another poster on this thread actually came close to this solution using ref
counts, but he basically looped through the list to count them O (n^2) for
each element rather then build the table.

hash tables would have solved in O(n) a few of the other questions they
asked me as well. I obviously knew about hash tables going in, but didn't
have it on the tip of my brain that operations were considered O(1). And I
don't know why I only used a hash table on the "2nd pass" while they were
grilling me...

But now I'm starting to see the wide array of uses for them .

Rud1ger Sch1erz
Guest
Posts: n/a

 11-28-2006
"Nobody" <(E-Mail Removed)> writes:

> basically the interview question is: given an unsorted listed such as:
>
> 3,1,3,7,1,2,4,4,3

Well, my solution takes 10 compares to find the first unique number (7
at position 3). The number of compares depends on the position, where
there first unique value is. In this example it takes
2 compares for value 0
2 compares for value 1
0 compares for value 2 (skipped, since already marked as not unique)
6 compares for value 3 (4 in rest of list, 2 in notuniquevalues)

Where can I get the job?

Sigh, I just *have to* fiddle around with those puzzles.

Cheers,
Rudiger

main.cc:
#include <iostream>

const size_t len = 9;
int values[len] = {3,1,3,7,1,2,4,4,3};
int notuniquevalues[len]; // array to remember already found not uniques
size_t ni = 0; // count of already found not unique values
bool stillunique[len] = {true, true, true, true, true, true, true, true, true};
int compare_count = 0;

bool IsUnique(int value, size_t pos, size_t len)
{
bool retval = false;

if(pos == len) {
retval = true;
for(int i = 0; i < ni; i++) {
compare_count++;
if(value == notuniquevalues[i]) {
retval = false;
break;
}
}
} else {
if(stillunique[pos]) {
compare_count++;
if(value == values[pos]) {
stillunique[pos] = false;
notuniquevalues[ni++] = value;
} else {
retval = IsUnique(value, pos+1, len);
}
} else {
retval = IsUnique(value, pos+1, len);
}
}

return retval;
}

int main(int argc, char** argv)
{
size_t pos =0;

while(pos < len) {
if(stillunique[pos]) {
if(IsUnique(values[pos], pos+1, len)) {
cout << "Found first unique " << values[pos] << " at position " << pos << endl;
cout << "Count of compares: " << compare_count << endl;
break;
}
}
pos++;
}

return 0;
}