Interview Question


Country: United States




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

Thanks for the question. We can solve it as the following:
Assume the following matrix:
1, 1, D, D, D, D
1, 1, D, D, D, D
1, 1, 1, D, D, D
1, 1, 1, 1, 1, D
1, 1, 1, 1, 1, D
1, 1, 1, 1, 1, 1

If you look at this matrix, you will see the tric here, for each row, we just need to check from the end position of '1' in the previous row.
Here in this example,
the end position of each row is 1, 1, 2, 4, 4, 5.
the end position is a non-decreasing sequence. So we end up checking from 1 - n. Then the runtime is O(n).

- ravio September 21, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thank you. Can you write the pseudocode if possible.

- inevitablekris September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

in the example you have taken number of 1's in row i is less than the number in row i+1, while the question says otherwise.
just pointing this out..it will not have much have on the answer though ( just reversing the loop condition)

- prateek September 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

count_ones(A, n):
	b = 0
	for (i = 0; i < n; ++i)
		if (A[n - 1][i] != '1') break
		++b

	c = b
	for (j = n - 2; j >= 0; --j)
		c += b
		for (i = b; i < n; ++i)
			if (A[j][i] != '1') break
			++b
			++c
	return c

- Anonymous September 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will give O(n^2) i want it in O(n)

- inevitablekris September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

This is O(n). The top for-loop will run n times to count the number of 1's in the n-1 row. The bottom for-loops will run from rows n-2 to 0 counting the 1's in those rows. However, the inner-loop will only count from b to n, where b is the number of 1's in the row below it. The worst case is when the number of 1's from n-1 to 0 increase by just one per row, forcing two comparisons per row (one for the new '1', and another for the 'D' next to it). The number of comparisons made can be represented by the relation, T(n) = T(n -1) + 2. The 2 is from two comparison.
T(n) = T(n - 1) + 2
T(n - 1) = T(n - 2) + 2
T(n - 2) = T(n - 3) + 2
-------------------------------
T(n - 1) = [T(n - 3) + 2] + 2 = T(n - 3) + 4
T(n) = [T(n - 3) + 4] + 2 = T(n - 3) + 6
T(n) = T(n - i) + 2i
T(1) = 1, since one element will only require one comparison
for i = n - 1,
T(n) = T(n - (n - 1)) + 2(n - 1) = T(1) + 2n - 2 = 1 + 2n -2 = 2n - 1 = O(n)
So, O(n) + O(n) = O(n)

- Anonymous September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You should observe that the number of comparisons (in the stated worst case) will be two (the new '1' in the current row plus the following 'D') per row. That is O(2 * n), where n is the number of rows.

- Andrew H September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

what will happened in this situation ?

1, 1, D, D, D, D
1, 1, 1, 1, 1, D
1, 1, 1, D, D, D
1, 1, 1, 1, 1, D
1, 1, 1, 1, 1, D
1, 1, 1, 1, 1, 1

- Guest September 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

That can't happen due to "number of 1's in row i is at least the number in row i+ 1"

- Anonymous September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks for clearing my doubt...

- Guest September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Or, more succinctly:

count_ones(A, n):
	b = 0
	c = 0
	for (j = n - 1; j >= 0; --j)
		c += b
		for (i = b; i < n; ++i)
			if (A[j][i] != '1') break
			++b
			++c
	return c

- Andrew H September 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countones(int arr[][], int n) {
    int prev = 0;
    int count = 0;
    for (int i = n-1; i > =0 ; i++) {
        num+=prev;
        for (int j = prev - 1; j < n; j++) {
            if (arr[i][j]) {
                num++;
                prev++;
            } else break;
       }
   }

- iwanna September 22, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will give O(n^2) i want it in O(n)

- inevitablekris September 22, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

{int count_ones(A, n)
{
	int num = 0, j=0;
	for(i=n-1;i>=0;i++)
	{
		while(A[i][j] != 'D')
			j++;
		j--;
		num += j;
	}
	return num;

}

- prateek September 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count = 0;
int column = 0;

for (int row = arr.length - 1; row > 0; row--) {
while (column <= arr.length - 1 && arr[row][column] == 1) {
count = count + row + 1;
column = column + 1;
}
}

- inevitablekris September 23, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is o(n).

- inevitablekris September 23, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int count1s(char[][] arr){
  int tot = 0;
  int pos = 0;
  for(char[] row : arr){
    while(pos < row.length && row[pos] == '1'){
      pos++;
    }
    tot += pos;
  }
  return tot;
}

- zortlord October 03, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int countingOnes(int[][] ones){
int size = ones.length();
int counter = 0;
for(int i=0;i<size;i++){
	//check for the last row because that will have max number of ones.
	if(ones[size-1][i]!='1') break;
	
	counter++;
}
	int t = counter;
	int j=n-2;
	while(j>=0){
	if(ones[j][b]!='1') break;
	b++;
	j++;
}
	}

}

- miniangel October 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry there are 2 typos:
1. There is an extra } at the end. 2. it should be j--. It works perfectly then, giving O(n)

- miniangel October 04, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

GIVEN A MULTIDIMENSIONAL ARRAY WITH VALUES SHOWN
40 1 3 270 21
6 73 44 10 5
72 127 99 82 72
WRITE A PROGRAM THAT OUTPUTS THE SMALLEST NUMBER IN EACH ROW

- Anonymous March 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

count_ones(A) { |MAX Times|
[row,col] = size(A); | 1 time | // Get rows and columns of Array A
result = 0; | 1 time |
i = 1 ; j = 1; | 1 time | // First element A(1,1)
for(i=1; i<=row; i++){ | N times |
while( A(i,j) != 0) |col times|
j++; |col times|
result += j; | N times |
j = 0; | N times |
}
return result; | 1 time |
}

- Lasithiotakis March 25, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

count_ones(A){			   |MAX Times|
    [row,col] = size(A);             	   | 1 time  |       // Get rows and columns of Array A
    result = 0;			   | 1 time  |
    i = 1 ; j = 1;		         	   | 1 time  |	  // First element A(1,1)
    for(i=1; i<=row; i++){	       	   | N times |
	while( A(i,j) != 0)      	   |col times|
	    j++;                   	   |col times|
	result += j;		   | N times |
	j = 0;			   | N times |
    }
    return result;		             | 1 time  |
}

- Anonymous March 25, 2017 | 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