## Interview Question for Software Engineer / Developers

Country: United States

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

RG, the original poster, please create a new thread for each study problem you need help with. Don't click "edit" on your previous study threads and put a new question in there.

Thanks!

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

I am able to solve the puzzle and answer is

3 2 10
4 0 6
5 12 1

Not clear on how to make it work programmatically ? Will it applicable to all the inputs?

The below points will helpful in writing program
1) Finding LCM of the given numbers to find the multiple of all sides.
2) Deciding on 2 diagonal corner numbers and deciding the other two diagonals
3) Adjusting the remaining 4 numbers in the middle places.

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

Your puzzle solution doesn't satisfy the following constraint:
"The sum of the east side is 3 more than the sum of the North Side"
3 + 2 + 10 = 15
10 + 6 + 1 = 17

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

Hey, I already figured out the solution. It is,

``````5   4   3
12       2
1   6   10``````

I need to figure out a way to solve this programatically.

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

I assumed Directions as like below and it met the rules.

E
3 2 10
N 4 0 6 S
5 12 1
W

Have I assumed the directions in a wrong way?

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

Directions are like this:

``````N
W        E
S``````

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

Thanks RG for correcting :-)

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

Use simulated annealing for larger NxN arrays.

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

Enumerate all possibilities and check, whatever the conditions. Simplest for such small problems.

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

Logic:
-Take 4 elements from given array for all combination out of 8 elements and assume these 4 elements are the 4 corner of 3X3 mattrix
[4 picked elements will represent in 2D mattrix as follows:]
[a , a, a, a]

-Now take rest of the 4 elements from array and check all the combination so that multiplication of all the phases will be equal
[rest of 4 elements will checked for below mentioned position]
[a, a, a, a]

-Now we got 4 phases with equal multiplication
a * a * a ==
a * a * a ==
a * a * a ==
a * a * a ==

-Now find the minimum sum phase and rotate mattrix in any direction until we get minimum sum phase at desired position

-And then we can perform left phase to right phase exchange or top phase to bottom phase exchange to get desired mattrix

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

exploring all permutations was easier for this. I wrote this in python and using the numpy library:

``````import numpy as np

class matrixPuzzle:
def __init__(self, arr):
self.arr = arr
self.matrix = np.zeros(shape=(3,3))
self.setTotal = set(x for x in arr)
self.setA = set()
self.setB = set()
self.Combs = ()
self.Perms = ()
self.solSet = []

def productValidate(self):
prodArr =  * 4 # preserve the products for R0, R2, C0, C2
setProd = set()
for i in xrange(3):
prodArr *= self.matrix[i] #R0
prodArr *= self.matrix[i] #R2
prodArr *= self.matrix[i] #C0
prodArr *= self.matrix[i] #C2

setProd = set(x for x in prodArr)
if len(setProd) == 1:
#print 'setProd=', setProd
return 1
else:
return 0

def sumValidate(self):
sumArr =  * 4
for i in xrange(3):
sumArr += self.matrix[i] #R0 (North)
sumArr += self.matrix[i] #R2 (South)
sumArr += self.matrix[i] #C0 (West)
sumArr += self.matrix[i] #C2 (East)
#Condition to satisfy: W - S = 1 # S - E = 2 # E - N = 3
if (sumArr - sumArr == 1) and (sumArr - sumArr == 2) and (sumArr - sumArr == 3):
return 1
else:
return 0

def findPerms(self, arr):
import itertools as it
#print('permutations')
P = it.permutations(arr, 4)
permPool = tuple(P)
return permPool

def solver(self):
self.Perms = self.findPerms(self.arr)
print 'len of perms:', len(self.Perms)
for tup in self.Perms:
#arrange the corner elements from each tuple in the Permutation
self.matrix = tup
self.matrix = tup
self.matrix = tup
self.matrix = tup

# run permutations of the other remaining array elements and fill up the matrix remaining positions
self.setA = set(x for x in tup)
self.setB = self.setTotal - self.setA
permPool = self.findPerms(self.setB)
for tupP in permPool:
self.matrix = tupP
self.matrix = tupP
self.matrix = tupP
self.matrix = tupP
#Now validate each matrix
retProd = self.productValidate()
if retProd == 1:
retSum = self.sumValidate()
if retSum == 1:
print 'product valid and sum valid:'
print self.matrix
# Capture the result which satisfies both conditions
self.solSet.append(self.matrix)

if __name__ == '__main__':
arr = [1,2,3,4,5,6,10,12]
mP = matrixPuzzle(arr)
mP.solver()``````

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

Isn't this an obvious solution? O(1) space and time complexity.

``````arr = [2, 1, 0, 4, 6, 3, 5]

i = 0
while i<len(arr):
t = arr[i]
arr[i] = arr[t]
arr[t] = t
i += 1

print arr``````

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

since arr[i] = arr[arr[i]] has to be satisfied you need to ensure that 0<=arr[i]<=n-1 where n is the size of the array.

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.