Interview Question


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

The permutations are just K-length "numbers" of radix-N (size-of-symbols), in the example 2-length numbers of radix 3, where the "digits" are 'A', 'B' and 'C' (0->A, 1->B, 2->C)

So if we create all K-lengths numbers of radix N we have solved the problem (just substiture digit-x with the xth symbol)

The number can be represented by an array of K integers, all 0 initially, this is number "00" that maps into "AA"

Now increment this number, the sequence generated will be (initial 00) then 01, 02, 10, 11, 12, 20, 21, 22 (all the 2-digit number using radix 3).

To increment the number represented using this array is a simple loop (adding one to the last digit, check for overflow, move the carry on, etc), same way as you have learnt in school. We know that we reached the highest representable number (in this case 22) when incrementing function will have a carry to the end.

This works properly if all the symbols are different and the number of symbols is smaller than the number that can be represented in an integer.

- Selmeczy, Péter December 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Agreed, this is also a brilliant technique

- Doctor.Sai December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This can be implemented using trees.

1. take the 1st element as root and add all the elements as a child.
for eg: take A as root, and add 3 child nodes: A, B, C

if the size = 3, again add child nodes to each sub node

in our example, with set = A,B,C and n = 3 , the tree would be:
A
/ | \
A B C
/|\ /|\ /|\
A B C ABC A B C

thus the height = k-1

Now traverse the tree from root to each leaf, which gives us the permutation
AAA, AAB and so on.

When you have traversed this tree, replace the node with next element from set, B in our case. repeat the process of traversal.

- Anonymous December 14, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

a little correction in last line:
When you have traversed this tree, replace the ROOT with next element from set, B in our case. repeat the process of traversal.

- Anonymous December 14, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Brilliant idea, one small development to this idea.Since the tree is going to be a complete n-array Tree(in our case ternary tree), it can be built using only arrays, just like in heap.
1. First allocate nCr array of bytes. and fill the first three places by A,B and C.
2. formula for calculating children is as follows.
3. for any element at an index i, its n children need to be placed at n*i+(n), n*i+(n+2),... n*i+(n+n-1).
4. fill each of its children's indices by the symbol array as long as these indices are within bounds of the array.
5. All that is left to do is Depth first traversal iteratively.

Repeat steps 1 to 5 starting with next elemennt form the Symbol array and so on.

- Doctor.Sai December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ops one small correction space allocation is n^size not nCsize or nCr

- Doctor.Sai December 15, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Ooooooooooooops again this is wrong. final and correct space calculation formula.
say n symbols are given and r spaces are allowed.
the space s = 1+n+n^2+n^3 + ... + n^(r-1). Now s will be the size of the array.

- Doctor.Sai December 15, 2011 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More