views:

266

answers:

6

i ran into thos quesiton in a google search.... they look pretty common, but i couldn't find a decent answer. any tips/links ?

1.Remove duplicates in array in O(n) without extra array

2.Write a program whose printed output is an exact copy of the source. Needless to say, merely echoing the actual source file is not allowed.

+4  A: 

For (1), you probably need more constraints than you've given. However, look up radix sort.

For (2), look up quine.

Greg Hewgill
Radix sort won't work because you aren't allowed an external array.
Blindy
+2  A: 

For your second question google for quine, you will find lots of answers!

Myth
+9  A: 

(1) isn't possible unless the array is presorted. The basic answer is to keep two pointers into the array, one walking forward searching for unequal elements, and one trailing pointer. When the forward pointer encounters an unequal element, it copies it into the trailing pointer and increments the trailing pointer.

(2) I don't have one handy. This sounds like a pretty terrible interview question. In most interpreted languages, a 0 byte (empty) source file is valid input, and prints out nothing.. that should count.

Terry Mahaffey
Heh, +1 for your answer to #2. Best way to answer such a pointless question :p
jalf
if the array is not presorted, it could be easily sorted in place using heap sort.
Anna
@Anna the question said "in O(n) time". Sorting the array can't be done faster than O(nlogn); hence my comment that the array must be presorted
Terry Mahaffey
Oops, didn't see that, you're absolutely right. +1 to you!
Anna
A: 

The closest you can get to one is using a hashtable to store seen elements and assigning each non-duplicated one to an appropriate value at the start of the array (this would leave several irrelevant ones at the end) - this would take O(n) time but is not the sort of thing you want to have to write during a job interview. Alternatively, as long as the list is sorted just check whether each element is equal to the previous.

For 2 would just manually printing the content's of the file be allowed? (if so the question is more than a little bit pointless).

Edit:

Here is a fast Perl version of my solution to the first - in c++ you would have to create the hash manually:

# Return an unsorted version of an array without duplicates
sub unsortedDedup {
    my %seen, my @return;

    map {
        $seen{$_} = 1 
        && push @return, $_ 
        unless (defined $seen{$_})
    } @_;

    @return;
}
ternaryOperator
A: 

The STL is often not an option in such interview questions, but here's one way to do #1 using the STL, although it does incur an additional sort (as explained by Terry's answer):

#include <iostream>
#include <algorithm>
#include <iterator>

int main()
{
  int a[] = { 2, 2, 3, 2, 1, 4, 1, 3, 4, 1 };
  int * end = a + sizeof(a) / sizeof(a[0]);

  std::sort(a, end);          // O(n log n)
  end = std::unique(a, end);  // O(n)

  std::copy(a, end, std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl;
}

Here's the result:

$ ./a.out 
1 2 3 4

std::unique() is generally implemented using the same technique Terry described in his answer (see bits/stl_algo.h in g++'s STL implementation for an example of how to implement it).

Void
A: 

For #2, there are a number of answers for different languages here: http://www.nyx.net/~gthompso/quine.htm

There is also an alternative quine in c++ here: http://npcomplete.weebly.com/1/post/2010/02/self-reproducing-c-program-quine.html

abc