Thursday, January 5, 2012

Calling all starving developers

If you are free this weekend (Friday evening to Sunday night) and still hungry, take part in Codesprint by Interviewstreet (a programmer database startup)

Participants will receive a set of engineering challenging and basic coding questions on Friday night and the participating tech companies, such as Skype, Apple, Zynga, etc, will recruit from the leaderboards. Remember fastest, most efficient code wins.

It is starting in 20hours time. Good luck!!!

Sample Question (from Interviewstreet):
Card Shuffling (30 Points)
Here is an algorithm for shuffling N cards:

1) The cards are divided into K equal piles.
2) The bottom N / K cards belong to pile 1 in the same order (so the bottom card of the initial pile is the bottom card of pile 1).
3) The next N / K cards from the bottom belong to pile 2, and so on.
4) Now the top card of the shuffled pile is the top card of pile 1. The next card is the top card of pile 2,..., the Kth card of the shuffled pile is the top card of pile K. Then (K + 1)th card is the card which is now at the top of pile 1, the (K + 2)nd is the card which is now at the top of pile 2 and so on.
For example, if N = 6 and K = 3, the order of a deck of cards "ABCDEF" (top to bottom) when shuffled once would change to "ECAFDB".

Given N and K, what is the least number of shuffles needed after which the pile is restored to its original order?

Input:
The first line contains the number of test cases T. The next T lines contain two integers each N and K.

Output:
Output T lines, one for each test case containing the minimum number of shuffles needed. If the deck never comes back to its original order, output -1.

Constraints:
K will be a factor of N.
T <= 10000
2 <= K <= N <= 10^9

Sample Input:
3
6 3
4 2
5 5

Sample Output:
6
4
2

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails