Linkedin Interview Question for Software Engineer / Developers


Country: United States




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

similar : stackoverflow_com/questions/20906214/permutation-algorithm-for-array-of-integers-in-java

- duskan February 20, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Recursive solution using python.

def generate(remaining):
  # Base cases
  if not remaining:
    return None
  
  if len(remaining) == 1:
    return [remaining]
  
  last_e = remaining[-1]
  subarray = remaining[0:-1]
  subsolutions = generate(subarray)  # Recursive call to solve a sub problem
  
  # Insert the last element in every single index
  new_solutions = []
  if subsolutions:
    for subsol in subsolutions:
      for i in range(len(subarray) + 1):  # Include the end of the list
        new_array = list(subsol)          # Have to copy the list in order to modify it
        new_array.insert(i, last_e)
        new_solutions.append(new_array)
  return new_solutions

x = generate([1, 2, 3])

- afernandezcmu March 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You need to consider whether there are duplicates

"""
Generate all permutations of given sequence
of elements.
Return a list of all distinct permutations.

E.g.
generate([1, 2, 3]) ->
[1, 2, 3], [1, 3, 2], [2, 3, 1],
[2, 1, 3], [3, 1, 2], [3, 2, 1]
"""


_results = []


def perm(A):
    global _results
    A = sorted(A)
    _results = []
    _aux([], A)
    return _results


def _aux(one_result, remain):
    global _results
    if not remain:
        _results.append(one_result)
    else:
        num = len(remain)
        for i in range(num):
            if i > 0 and remain[i - 1] == remain[i]:
                continue
            else:
                _aux(one_result + [remain[i]],
                     remain[:i] + remain[i + 1:])

- shoudaw April 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

package linkedin;

import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class Permutations {
List<int[]> expected = Arrays.asList(
new int[]{1, 2, 3},
new int[]{2, 1, 3},
new int[]{2, 3, 1},
new int[]{1, 3, 2},
new int[]{3, 1, 2},
new int[]{3, 2, 1}
);

@Test
public void test() {
List<int[]> actual = permutations(new int[]{1, 2, 3}, 0);
assertEquals(expected.size(), actual.size());

Iterator<int[]> actualIt = actual.iterator();
Iterator<int[]> expectedIt = expected.iterator();
while (actualIt.hasNext()) {
assertArrayEquals(expectedIt.next(), actualIt.next());
}
}

private List<int[]> permutations(int[] a, int s) {
if (a.length == 0 || s == a.length) return Collections.emptyList();
if (s == a.length - 1) return Collections.singletonList(new int[]{a[s]});
List<int[]> permsAfter = permutations(a, s + 1);
List<int[]> res = new ArrayList<>(permsAfter.size() * (permsAfter.get(0).length + 1));
for (int[] permAfter : permsAfter) {
for (int i = 0; i < permAfter.length + 1; i++) {
int[] perm = new int[permAfter.length + 1];
for (int j = 0; j < perm.length; j++) {
perm[j] = (i == j ? a[s] : (i < j ? permAfter[j - 1] : permAfter[j]));
}
res.add(perm);
}
}
return res;
}
}

- Boris Marchenko July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

With formatting.

package linkedin;

import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class Permutations {
    List<int[]> expected = Arrays.asList(
            new int[]{1, 2, 3},
            new int[]{2, 1, 3},
            new int[]{2, 3, 1},
            new int[]{1, 3, 2},
            new int[]{3, 1, 2},
            new int[]{3, 2, 1}
    );

    @Test
    public void test() {
        List<int[]> actual = permutations(new int[]{1, 2, 3}, 0);
        assertEquals(expected.size(), actual.size());

        Iterator<int[]> actualIt = actual.iterator();
        Iterator<int[]> expectedIt = expected.iterator();
        while (actualIt.hasNext()) {
            assertArrayEquals(expectedIt.next(), actualIt.next());
        }
    }

    private List<int[]> permutations(int[] a, int s) {
        if (a.length == 0 || s == a.length) return Collections.emptyList();
        if (s == a.length - 1) return Collections.singletonList(new int[]{a[s]});
        List<int[]> permsAfter = permutations(a, s + 1);
        List<int[]> res = new ArrayList<>(permsAfter.size() * (permsAfter.get(0).length + 1));
        for (int[] permAfter : permsAfter) {
            for (int i = 0; i < permAfter.length + 1; i++) {
                int[] perm = new int[permAfter.length + 1];
                for (int j = 0; j < perm.length; j++) {
                    perm[j] = (i == j ? a[s] : (i < j ? permAfter[j - 1] : permAfter[j]));
                }
                res.add(perm);
            }
        }
        return res;
    }
}

- Boris Marchenko July 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Permutation {

	private List<List<Integer>> results = new ArrayList<List<Integer>>();
	
	
	public void perm(List<Integer> result, List<Integer> remaining) {
		
		if(remaining.size() == 0) {
			results.add(result);
		}
		
		for (int i=0; i<remaining.size(); i++) {
			if(i>0 && remaining.get(i-1) == remaining.get(i)) {
				continue;
			}
			
			List<Integer> copy1 = new ArrayList<Integer>(result);
			copy1.add(remaining.get(i));
			List<Integer> copy2 = new ArrayList<Integer>(remaining);
			copy2.remove(i);
			perm(copy1, copy2);
		}
	}
	
	public void print() {
		for(List<Integer> result : results) {
			for(Integer i : result) {
				System.out.print(i + " ");
			}
			System.out.println();
		}
	}
	
	public static void main(String[] args) {
		Permutation p = new Permutation();
		List<Integer> input = Arrays.asList(1,1,2,3);
		p.perm(new ArrayList<Integer>(), input);
		p.print();
	}

}

- nafan August 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

bool perm_next (int *a, int n)
{   
    int k = n - 2;

    if (k < 0) return false;

    // Find the largest index k such that a[k] < a[k + 1]. If no such index
    // exists, the permutation is the last permutation.
    //
    while (a[k] >= a[k+1]) {
        k--; 
        if (k < 0)
            return false;
    }

    // Find the largest index l such that a[k] < a[l]. Since k + 1 is such an
    // index, l is well defined and satisfies k < l.
    int l = n - 1;
    while (a[k] >= a[l])
        l--;

    // Swap a[k] with a[l]
    std::swap(a[k], a[l]);

    // Reverse the sequence from a[k + 1] up to and including the final
    // element a[n]
    std::reverse(&a[k+1], &a[n]);

    return true;
}

int main()
{
    int a[N];

    for (int i = 0; i < N; i++)
        a[i] = i + 1;

    do {
        print(a, N);
    } while (perm_next(a, N));

    return 0;
}

- Westlake February 19, 2014 | Flag Reply


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