Idan
Idan

Reputation: 5817

interview questions - little help

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.

Upvotes: 3

Views: 948

Answers (6)

abc
abc

Reputation: 1342

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

Upvotes: 0

Void - Othman
Void - Othman

Reputation: 3481

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).

Upvotes: 0

ternaryOperator
ternaryOperator

Reputation: 833

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;
}

Upvotes: 0

Terry Mahaffey
Terry Mahaffey

Reputation: 11981

(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.

Upvotes: 10

Myth
Myth

Reputation: 1602

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

Upvotes: 3

Greg Hewgill
Greg Hewgill

Reputation: 993881

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

For (2), look up quine.

Upvotes: 5

Related Questions