views:

2543

answers:

15

Not so long ago I was in an interview, that required solving two very interesting problems. I'm curious how would you approach the solutions.

Problem 1 :

Product of everything except current

Write a function that takes as input two integer arrays of length len, input and index, and generates a third array, result, such that: result[i] = product of everything in input except input[index[i]]

For instance, if the function is called with len=4, input={2,3,4,5}, and index={1,3,2,0}, then result will be set to {40,24,30,60}.

IMPORTANT: Your algorithm must run in linear time.

Problem 2 : ( the topic was in one of Jeff posts )

Shuffle card deck evenly

  1. Design (either in C++ or in C#) a class Deck to represent an ordered deck of cards, where a deck contains 52 cards, divided in 13 ranks (A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K) of the four suits: spades (?), hearts (?), diamonds (?) and clubs (?).

  2. Based on this class, devise and implement an efficient algorithm to shuffle a deck of cards. The cards must be evenly shuffled, that is, every card in the original deck must have the same probability to end up in any possible position in the shuffled deck. The algorithm should be implemented in a method shuffle() of the class Deck: void shuffle()

  3. What is the complexity of your algorithm (as a function of the number n of cards in the deck)?

  4. Explain how you would test that the cards are evenly shuffled by your method (black box testing).

P.S. I had two hours to code the solutions

+1  A: 

For the first one, first calculate the product of entire contents of input, and then for every element of index, divide the calculated product by input[index[i]], to fill in your result array.

Of course I have to assume that the input has no zeros.

Vaibhav
A: 

Vaibhav, unfortunately we have to assume, that there could be a 0 in the input table.

+6  A: 

First question:

int countZeroes (int[] vec) {
int ret = 0;
foreach(int i in vec) if (i == 0) ret++;

return ret;
}

int[] mysticCalc(int[] values, int[] indexes) {
    int zeroes = countZeroes(values); 
    int[] retval = new int[values.length];
    int product = 1;

    if (zeroes >= 2) { // 2 or more zeroes, all results will be 0
     for (int i = 0; i > values.length; i++) {
      retval[i] = 0;  
     }
     return retval;
    }
    foreach (int i in values) {
     if (i != 0) product *= i; // we have at most 1 zero, dont include in product;
    }
    int indexcounter = 0;
    foreach(int idx in indexes) {
     if (zeroes == 1 && values[idx] != 0) {  // One zero on other index. Our value will be 0
      retval[indexcounter] = 0;
     }
     else if (zeroes == 1) { // One zero on this index. result is product
      retval[indexcounter] = product;
     }
     else { // No zeros. Return product/value at index
      retval[indexcounter] = product / values[idx];
     }
     indexcouter++;
    } 
    return retval;
}

Worst case this program will step through 3 vectors once.

Tnilsson
Hmm, should the last else (no zeroes) not have,retval[indexcounter] = product/values[index[i]] ?
viksit
Gah, I meant "indexes" according to this program for the above comment, not "index".
viksit
+1  A: 

Product of everything except current in C

void product_except_current(int input[], int index[], int out[], 
                            int len) {
  int prod = 1, nzeros = 0, izero = -1;

  for (int i = 0; i < len; ++i) 
    if ((out[i] = input[index[i]]) != 0)
      // compute product of non-zero elements 
      prod *= out[i]; // ignore possible overflow problem
    else {
      if (++nzeros == 2) 
         // if number of zeros greater than 1 then out[i] = 0 for all i
         break; 
      izero = i; // save index of zero-valued element
    }

  //  
  for (int i = 0; i < len; ++i)  
    out[i] = nzeros ? 0 : prod / out[i];                               

  if (nzeros == 1)
    out[izero] = prod; // the only non-zero-valued element
}
J.F. Sebastian
A: 

Tnilsson, great solution ( because I've done it the exact same way :P ).

I don't see any other way to do it in linear time. Does anybody ? Because the recruiting manager told me, that this solution was not strong enough.

Are we missing some super complex, do everything in one return line, solution ?

YXJuLnphcnQ has pretty much the same algotithm, but he does it in C with everything that means. What he has done that is arguably faster is to calculate zeroes and product in the same loop.
Tnilsson
You two are iterating an extra time over the array to count zeroes... I wrote basically the same YXJuLnphcnQ did but in python, didn't bother to post it as there were already three solutions
Vinko Vrsalovic
Also, linear time, i.e. O(n), isn't the same as single pass.
Unsliced
Or maybe none of us is strong enough to work at that company, a very real possibility
Vinko Vrsalovic
No, o(n) isnt the same as a single pass. Err, if I could explain big-O calculus in 300 characters I would be rich. Allow me to get away with - No, it isnt.
Tnilsson
I really doubt that an extra pass is the difference between not strong enough and strong enough. BTW O() is the asymptotic upper bound to the growth of execution time of an algorithm with respect to the input size (n). So, O(n) means that the execution time grows at most as n grows (plus constants).
Vinko Vrsalovic
Why am I not rich?
Vinko Vrsalovic
A: 

Second problem.

    public static void shuffle (int[] array) 
    {
        Random rng = new Random();   // i.e., java.util.Random.
        int n = array.length;        // The number of items left to shuffle (loop invariant).
        while (n > 1) 
        {
            int k = rng.nextInt(n);  // 0 <= k < n.
            n--;                     // n is now the last pertinent index;
            int temp = array[n];     // swap array[n] with array[k] (does nothing if k == n).
            array[n] = array[k];
            array[k] = temp;
        }
    }

This is a copy/paste from the wikipedia article about the Fisher-Yates shuffle. O(n) complexity

Tnilsson
A: 

Tnilsson, I agree that YXJuLnphcnQ solution is arguably faster, but the idee is the same. I forgot to add, that the language is optional in the first problem, as well as int the second.

You're right, that calculationg zeroes, and the product int the same loop is better. Maybe that was the thing.

It's faster. Probably not much faster, but still. I would still write it approximately as I did, since I believe it is clearer
Tnilsson
A: 

Tnilsson, I've also uset the Fisher-Yates shuffle :). I'm very interested dough, about the testing part :)

Hmm. Testing... That one I need to think about for a bit. You cant test by just shuffling once or twice, it needs to be done a massive ammounts of time (And I'm prepared to claim that the one that says it can be done after one shuffle does not understand randomness)
Tnilsson
I "stole" that part of the question and made a new one at http://beta.stackoverflow.com/questions/56411/how-to-test-randomness-case-in-point-shuffling. It's a tricky one indeed. I hope you dont mind
Tnilsson
A: 

Shuffle card deck evenly in C++

#include <algorithm>

class Deck {
  // each card is 8-bit: 4-bit for suit, 4-bit for value
  // suits and values are extracted using bit-magic
  char cards[52];
  public:
  // ...
  void shuffle() {
    std::random_shuffle(cards, cards + 52);
  }
  // ...
};

Complexity: Linear in N. Exactly 51 swaps are performed. See http://www.sgi.com/tech/stl/random_shuffle.html

Testing:

  // ...
  int main() {
    typedef std::map<std::pair<size_t, Deck::value_type>, size_t> Map;
    Map freqs;    
    Deck d;
    const size_t ntests = 100000;

    // compute frequencies of events: card at position
    for (size_t i = 0; i < ntests; ++i) {
      d.shuffle();
      size_t pos = 0;
      for(Deck::const_iterator j = d.begin(); j != d.end(); ++j, ++pos) 
        ++freqs[std::make_pair(pos, *j)]; 
    }

    // if Deck.shuffle() is correct then all frequencies must be similar
    for (Map::const_iterator j = freqs.begin(); j != freqs.end(); ++j)
      std::cout << "pos=" << j->first.first << " card=" << j->first.second 
                << " freq=" << j->second << std::endl;    
  }

As usual, one test is not sufficient.

J.F. Sebastian
A: 

Trilsson made a separate topic about the testing part of the question

http://stackoverflow.com/questions/56411/how-to-test-randomness-case-in-point-shuffling

very good idea Trilsson:)

A: 

YXJuLnphcnQ, that's the way I did it too. It's the most obvious.

But the fact is, that if you write an algorithm, that just shuffles all the cards in the collection one position to the right every time you call sort() it would pass the test, even though the output is not random.

I agree that one test is not sufficient. Additional test are required. One test is better than nothing.
J.F. Sebastian
+1  A: 

A linear-time solution in C#3 for the first problem is:-

IEnumerable<int> ProductExcept(List<int> l, List<int> indexes) {
    if (l.Count(i => i == 0) == 1) {
        int singleZeroProd = l.Aggregate(1, (x, y) => y != 0 ? x * y : x);
        return from i in indexes select l[i] == 0 ? singleZeroProd : 0;
    } else {
        int prod = l.Aggregate(1, (x, y) => x * y);
        return from i in indexes select prod == 0 ? 0 : prod / l[i];
    }
}

Edit: Took into account a single zero!! My last solution took me 2 minutes while I was at work so I don't feel so bad :-)

kronoz
+1  A: 

Here's the answer to the second one in C# with a test method. Shuffle looks O(n) to me.

Edit: Having looked at the Fisher-Yates shuffle, I discovered that I'd re-invented that algorithm without knowing about it :-) it is obvious, however. I implemented the Durstenfeld approach which takes us from O(n^2) -> O(n), really clever!

public enum CardValue { A, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, J, Q, K }
public enum Suit { Spades, Hearts, Diamonds, Clubs }

public class Card {
    public Card(CardValue value, Suit suit) {
        Value = value;
        Suit = suit;
    }

    public CardValue Value { get; private set; }
    public Suit Suit { get; private set; }
}

public class Deck : IEnumerable<Card> {
    public Deck() {
        initialiseDeck();
        Shuffle();
    }

    private Card[] cards = new Card[52];

    private void initialiseDeck() {
        for (int i = 0; i < 4; ++i) {
            for (int j = 0; j < 13; ++j) {
                cards[i * 13 + j] = new Card((CardValue)j, (Suit)i);
            }
        }
    }

    public void Shuffle() {
        Random random = new Random();

        for (int i = 0; i < 52; ++i) {
            int j = random.Next(51 - i);
            // Swap the cards.
            Card temp = cards[51 - i];
            cards[51 - i] = cards[j];
            cards[j] = temp;
        }
    }

    public IEnumerator<Card> GetEnumerator() {
        foreach (Card c in cards) yield return c;
    }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
        foreach (Card c in cards) yield return c;
    }
}

class Program {
    static void Main(string[] args) {
        foreach (Card c in new Deck()) {
            Console.WriteLine("{0} of {1}", c.Value, c.Suit);
        }

        Console.ReadKey(true);
    }
}
kronoz
In that shuffle, can every card possibly stay put? That could be a problem with your solution, e.g. that card[51] will get moved and cannot ever land back so that it has an even probability of ending up anywhere in the deck.
JB King
+2  A: 

Product of everything except current in Python

from numpy import array

def product(input, index):
    a = array(input)[index]

    if a[a == 0].size != 1:
        a = a.prod() / a # product except current
    else:
        # exaclty one non-zero-valued element in `a`
        nzi = a.nonzero() # indices of non-zero-valued elements
        a[a == 0] = a[nzi].prod()
        a[nzi] = 0

    return a

Example:

for input in ([2,3,4,5], [2,0,4,5], [0,3,0,5]):
    print product(input, [1,3,2,0])

Output:

[40 24 30 60]
[40  0  0  0]
[0 0 0 0]
J.F. Sebastian
A: 

In Haskell:

import Array

problem1 input index = [(left!i) * (right!(i+1)) | i <- index]
  where left  = scanWith scanl
        right = scanWith scanr
        scanWith scan = listArray (0, length input) (scan (*) 1 input)
Darius Bacon