## Adobe Interview Question for Computer Scientists

Country: India
Interview Type: In-Person

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

``````Number the words as has been done in the previous comment
Initial configuration is {5,2,3,1,4,6}
Final configuration is {1,2,3,4,5,6}

Constructing a graph with edges between final position & initial position we have
1->5
2->2
3->3
4->1
5->4
6->6

1->5->4->1 , 2->2 , 3->3 , 6->6

The answer will be the total number of nodes - the number of disconnected components

This can be explained as follows,
In a component(which is always a cycle) of 'n' nodes, n-1 swaps would be needed``````

Comment hidden because of low score. Click to expand.
0

What if there are repetitions, 123456 don't have any repetitions but in english text alphabets would repeat.

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

Can the swaps occur anywhere, or for adjacent words only?

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

Just as you said, it is " similar to minimum swaps to convert string A to string B." so would we be given the string B in this case, which is "we must go to newyork now"?

1. If yes, then the decisive solution is same as conversion of string A to string B which takes some help from the Longest Common Sub-sequence dynamic prog problem.

2. If no, then I don't see if there is any decisive solution..

Comment hidden because of low score. Click to expand.
0

How is this remotely related to DP?

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

we can use inversion count approach
step 1: assign a number t each word of string A
and step 2: apply inversion count
geeksforgeeks archive : archives/3968

Comment hidden because of low score. Click to expand.
0

Can you pls explain in detail...

Comment hidden because of low score. Click to expand.
0

Yeah, true...

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

good solution bhavesh
We can assign a number to each word in the correct sentence
e.g.:
we must go to newyork now
we - 1
must - 2
go - 3
to - 4
newyork - 5
now - 6

do this using a map
now for the senetence spoken by yoda
"Newyork must go we to now"
create an int array storing numbers for the corresponding words
array would be {5,2,3,1,4,6}

count the number of inversions as done here: www[dot]geeksforgeeks[dot]org/archives/3968

Comment hidden because of low score. Click to expand.
0

Though this is nice approach, finding inversion wont give you minimum swaps required.but we can get minimum swaps form this new array.

Comment hidden because of low score. Click to expand.
0

yeah... sorry

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

The question of minimum number of swaps can be asked only if the resultant sentence(correct one) is given.

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

You see this in your mirror reflection

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.

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