VMWare Inc Interview Question for Software Engineer in Tests






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

I think the idea is using a method that has an O(1) in finding the number of ones ....
So may be we should have precomputed 256 values in of a 8bits byte and inserted inside a Hash or Lookup Table. then if the array has a size of N, we can do it in O(N).

- moussa April 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count=0
for(byte i : array){
for(int i=0;i<8;i++){
if (i&1<<i==1) count++;
}
}

- Anonymous February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it should be
"if (i & 1<<i > 0) count++;"

- Daru February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please explain the logic behind this ?
And my first question is how a byte element can have value 000010000111 ??

- Piyush March 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int counteOne(int[] arr){
if(arr.length < 0){
return 0;
}
int counter = 0;
for(int i = 0; i < arr.length; i++){
if(arr[i] ==1){
counter ++;
}
}
return counter;
}

- Sanjay Kumar (Inventwheel.com) February 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sanjay,
I don't think we can iterate a byte array with int. line arr[i]. We need some differnet operations for bytes.

- newbee February 26, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

long count_bits(byte n) {
int c; // c accumulates the total bits set in n
for (c = 0; n > 0; c++)
n &= n - 1; // clear the least significant bit set
return c;
}

- Shailesh Kushwaha March 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int count=0
for(byte i : array){
for(int j=0;j<8;++){
if (i>>>j==1) count++;
}
}

- shoushou May 08, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int getCountOfSetBits(byte byteArr[]) {
int count = 0;
for (byte b : byteArr) {
while (b != 0) {
b = (byte) (b & (b - 1));
++count;
}
}
return count;
}

- katrix July 02, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

byte[] array = {0,0,0,0,1,1,0,1,0,1,1,1,1,0,0,1,0,1,0};
int count = 0;
for(int i=0;i<array.length;i++) {
if(new Byte(array[i]).intValue() == 1) {
count++;
}
}

- Sree July 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

unsigned char table[256];
table[0] = 0;
for(int i = 0; i < 256; i++) {
table[i] = (i & 1) + table[i/2];
}

- Anonymous August 23, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int countOne(byte[] arr)
{
int count=0;

for (int i = 0; i < arr.length; i++)
{
byte b = 1;
if(arr[i]==b)
{
count++;

}



}

return count;
}

- Dharmesh January 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
		int count = 0;
		byte[] arr = {3,4,1,5,3,9};
		for(byte single : arr){
			while(single!=0){
				if((single & 1 ) == 1)
					count++;
				single = (byte)((single & 0xff) >>> 1);
			}
		}
			
		System.out.println(count);

}

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

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

public class ArrayBytes {
public static void main(String[] args) {
List<String> arrayOfBytes = new ArrayList<String>(5);
// sample binary number starting from 10 to 15
// will be initially stored in array-
for (int j = 10; j < 15; j++) {
arrayOfBytes.add(Integer.toBinaryString(j));
}
long count = 0;
for (String string : arrayOfBytes) {
System.out.println(string);

char[] charArray = string.toCharArray();

for (char c : charArray) {
if (c == '1') {
count++;
}
}
}
// total count of 1 in all the elements of the array printed here
System.out.println(count);
}
}

- surya.n.lokhande October 27, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In java i have:

public static int numOfOnes(byte[] byteArr){
	int count = 0;
	for(byte b: byteArr)
		count += countOnes(b);
	return count;
}

private static int countOnes(byte b){
	int count=0;
	for (int i=0; i<8; i++)
		count+= (b>>i)&1;
	return count;
}

This should take up O(1) space and O(n) time complexity.

- hengel1993 December 22, 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