Microsoft interview question

Given a 2D array of boolean values determine the largest square sub-array containing only 1s.

Interview Answers

Anonymous

6 Jun 2014

Its a dynamic programming problem

Anonymous

22 Feb 2015

int[][] arr = new int[][] { {0, 0, 0, 0}, {1, 1, 1, 1}, {1, 1, 1, 1, 1, 1, 1, 1, 1}, {1, 1, 0, 1} }; int prev_len = 0; int index = 0; for(int i = 0; i < arr.length; i++) { int sq_rt = (int) Math.sqrt(arr[0].length); if(Math.sqrt(arr[0].length) - sq_rt == 0) { int count = 0; for(int j = 0; j < arr[i].length; j++) { if(arr[i][j] == 1) { count++; if(count == arr[i].length) { prev_len = arr[i].length; index = i; } } else { break; } } } } System.out.println("The largest square sub-array containing 1s is at index " +index+ " and its length is " +prev_len);

Microsoft Interview Question: Given a 2D array of boolean values determine the largest square sub-array containing only 1s. | Glassdoor