Amazon Interview Question for SDE-2s


Country: India
Interview Type: In-Person




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

another solution. prints all possible combinations for a given N (else prints nothing)

void findLangford(int[] arr, int n) {
        if (n == 0) {
            for (int i = 0; i< arr.length;i++)
                System.out.print(arr[i] + " ");
            System.out.print("\n");
            return;
        }
        for (int i=0;i<arr.length-n-1;i++) {
            if(arr[i] == 0 && arr[i+n+1] == 0) { //empty slot
                arr[i] = n; arr[i+n+1] = n;
                findLangford(arr, n-1);
                arr[i] = 0; arr[i+n+1] = 0; //undo
            }
        }
    }

EDIT: the array should be of size 2*N - check can be added

- VJ April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain what you have done?

- alex April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

trivial recursive implementation:

public class LangfordPairing {

	private int[] internal;
	private int len;
	
	public LangfordPairing( int n ) {
		len = n * 2;
		internal = new int[len];
		for( int i = 0; i < 2*n; i++ ) {
			internal[i] = 0;
		}
	}
	
	public boolean doPairingHelper( int n, int startPosition ) {
		if( startPosition < 0 || startPosition + n + 2 > len ) {
			return false;
		}
		
		if( n == 1 ) {
			int pos = getFirstNonZeroPosition(0);
			if( pos != -1 && pos+n+1 < len && internal[pos] == 0 && internal[pos+n+1] == 0 ) {
				internal[pos] = n;
				internal[pos+n+1] = n;
				return true;
			}
			return false;
		}else {
			int pos = getFirstNonZeroPosition( startPosition );
			if( pos != -1 && pos+n+1 < len && internal[pos] == 0 && internal[pos+n+1] == 0 ) {
				internal[pos] = n;
				internal[pos+n+1] = n;
				if( doPairingHelper( n-1, 0 ) ) {
					return true;
				}else {
					internal[pos] = 0;
					internal[pos+n+1] = 0;
				}
			}
			return doPairingHelper( n, startPosition+1 );
		}	
	}
	
	public boolean doPairing() {
		return doPairingHelper( len/2, 0 );
	}
	
	private int getFirstNonZeroPosition( int start ) {
		if( start >= 0 && start < len ) {
			for( int i = start; i < len; i++ ) {
				if( internal[i] == 0 ) {
					return i;
				}
			}						
		}
		return -1;
	}
	
	public int[] getResult() {
		return internal;
	}
	
	public static void main( String[] args ) {
		LangfordPairing lf = new LangfordPairing( 8 );
		if( lf.doPairing() ) {
			int[] result = lf.getResult();
			for( int i : result ) {
				System.out.print( i + ", " );
			}
		}else {
			System.out.println( "Failed!" );
		}
	}
}

- zy April 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Langford {
    private static boolean print(int a[], int k, int n) {
        if (k == n + 1) {
            return true;
        }
        for (int i = 0; i < a.length; ++i) {
            if (a[i] == 0 && i + k + 1 < a.length && a[i + k + 1] == 0) {
                a[i] = a[i + k + 1] = k;
                if (print(a, k + 1, n)) {
                    return true;
                }
                a[i] = a[i + k + 1] = 0;
            }
        }
        return false;
    }
    public static void print(int n) {
        int a[] = new int[n << 1];
        boolean ret = print(a, 1, n);
        if (ret) {
            for (int i = 0; i < a.length; ++i) {
                System.out.print(a[i] + " ");
            }
            System.out.println();
        } else {
            System.out.println("Sorry");
        }
    }
    public static void main(String args[]) {
        print(Integer.parseInt(args[0]));
    }
}

- rixcat April 17, 2013 | 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