Amazon Interview Question

Online coding interview: Given an array of integers. Find the largest increasing sub sequence of integers in the array. // 10, 3, 7, 9, 0, 15 // return index 1&3

Interview Answers

Anonymous

Sep 25, 2014

/** * @param numArr Array of ints * @return start index and end index of the longest increasing sub sequence */ static int[] findMaxIncSubSeq(int[] numArr) { int[] increments = new int[numArr.length]; increments[0] = 1; for (int i = 1; i = numArr[i - 1]) { increments[i] = increments[i - 1] + 1; } else { increments[i] = 0; } } int max = increments[0]; int maxIdx = 0; for (int i = 0; i max) { max = increments[i]; maxIdx = i; } } return new int[]{maxIdx - max, maxIdx}; }

3

Anonymous

Nov 9, 2014

public class Main{ public static void main(String []args){ Integer[] values1 = {1,2,3,4,5}; Integer[] values2 = {10, 3, 7, 9, 0, 15}; Integer[] values3 = {2, 7, 7, 9, 0, 15}; Integer[] values4 = {10, 9, 8, 9, 6, 5}; findMaxSequence(values1); findMaxSequence(values2); findMaxSequence(values3); findMaxSequence(values4); } private static void findMaxSequence(Integer[] values) { assert(values.length > 0); int length = 1; int maxLength = length; int index = 0; int indexMaxLength = index; for(int ind = 1; ind maxLength) { maxLength = length; indexMaxLength = index; } index = ind; length = 1; } else { length++; } } //Check for the sequence at the end of the array if(length > maxLength) { maxLength = length; indexMaxLength = index; } System.out.println("Max Sequence Index: " + indexMaxLength); System.out.println("Max Sequence Length: " + maxLength); } }

Anonymous

Dec 24, 2014

package com.coding.tests; public class LongestIncreasingSubSeq { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] testCase = {10, 3, 7, 9, 0, 15,1,2,1,4,12,23,45,56,78,7,1}; int[] res = getLongestIncreasingSubSeq(testCase); for(int num : res){ System.out.print(num+" "); } } public static int[] getLongestIncreasingSubSeq(int[] arr){ if(arr==null || arr.length=arr[i-1]){ currentOne.length++; }else{ if(currentOne.length>=maxOne.length){ maxOne.length = currentOne.length; maxOne.start = currentOne.start; } currentOne.start=i; currentOne.length=1; } } if(currentOne.length>=maxOne.length){ maxOne.length = currentOne.length; maxOne.start = currentOne.start; } int[] res = new int[maxOne.length]; System.arraycopy(arr, maxOne.start, res, 0, maxOne.length); return res; } private static class SubIncreasingSeq{ int start; int length; } }

Anonymous

Feb 28, 2015

package test; public class LargestIncSubSeq { public static void main(String[] args) { int start = 0,end = 0 ,larSeqStart = 0 ,larSeqEnd = 0; int [] array = {10,3,7,9,0,15,1,2,3,4,5}; // 0 1 2 3 4 5 6 7 8 9 10 for (int i=1 ;i (end -start+1)){ //current lar seq d one start = end = i; } else{ //start & end is largest seq shift it larSeqStart = start; larSeqEnd = end; start = end = i; } } } if((larSeqEnd -larSeqStart+1) < (end -start+1)){ //start & end is largest seq shift it larSeqStart = start; larSeqEnd = end; } System.out.println("largest seq"+larSeqStart +" "+larSeqEnd); } }

Anonymous

May 9, 2015

//assuming ar.Length > 0 private static void solve(int[] ar) { var longest_len = 1; var longest_left_idx = 0; var longest_right_idx = 0; var left_idx = 0; for (int i = 1; i ar[i - 1]) { if (longest_len < i - left_idx) { longest_left_idx = left_idx; longest_right_idx = i; longest_len = i - left_idx; } } else { left_idx = i; } } Console.WriteLine(longest_left_idx); Console.WriteLine(longest_right_idx); }

Anonymous

Aug 2, 2018

static String findSeq(int[] arr){ if(null == arr || arr.length=0){ return ""; } int j=0; int max=0; String result = "0";int count=0; for(int i=0;i max){ max = count; result=j+","+(i+1); } }else{ j=i+1; count=0; } } return result; }