xyz Interview Question for SDE-2s


Country: United States
Interview Type: Phone Interview




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

static int get1DIndex(int[] ds,int [] is)
  {
    int ret=0;
  
    for(int j=0;j<ds.length;j++)
    {
      if(j>0)
        ret=ret*ds[j]+is[j];
      else
        ret=is[j];
    }
    return ret;
  }
  static int[] getDDIndex(int [] ds,int index)
  {
    int [] ret=new int[ds.length];
    for(int j=ds.length-1;j>=0;j--)
    {
      if(j>0)
      {
        ret[j]=index%ds[j-1];
        index=index/ds[j-1];
      }
      else
        ret[j]=index;
    }
    return ret;
  }
  static void clearIsland(boolean[] data,int[]ds, boolean[] visited,int[] index)
  {
    int i1d=get1DIndex(ds,index);
    if(visited[i1d]|| !data[i1d])
      return;
    visited[i1d]=true;
    for(int i=0;i<index.length;i++)
    {
      int j=index[i];
      if(j-1>=0)
      {
        index[i]=j-1;
        clearIsland(data,ds,visited,index);
      }
      if(j+1<ds[i])
      {
        index[i]=j+1;
        clearIsland(data,ds,visited,index);
      }
      index[i]=j;
    }
  }
  public static int getNIslands(boolean[] data,int[] ds)
  {
    int size=1;
    for(int d:ds)
      size*=d;
    if(data.length!=size)
      throw new IllegalArgumentException();
      
    boolean[] visited=new boolean[data.length];
    for(int i=0;i<visited.length;i++)
    {
      visited[i]=false;
    }
    int island=0;
    for(int i=0;i<data.length;i++)
    {
      if(!data[i] || visited[i])
        continue;
      island++;
      clearIsland(data,ds,visited,getDDIndex(ds,i));
    }
    return island;
  }

- tewelde May 24, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do they consider starting and ending bits to be islands if their respective following and prior elements in an array are different?

- Anonymous May 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The idea here is to parse the matrix diagnolly

int b[4][4] = { { 1,1,1,0 },{ 0,0,1,0 },{ 0,0,1,0 },{ 0,0,0,0 } };

bool check1(int row,int col)
{
	if (col == 0 && row != 0)
	{
		if (b[row - 1][col] == 1)
			return true;
	}
	else if (row == 0 && col != 0)
	{
		if (b[row][col-1] == 1 || b[row + 1][col - 1] == 1)
			return true;
	}
	else if (row == 0 && col == 0)
		return false;
	else if (b[row + 1][col - 1] == 1 || b[row][col - 1] == 1 || b[row - 1][col] == 1)
		return true;

	return false;
}

int main()
{
	int a[] = { 9,3,8,11,12,4,7,0,7,8 };
	int islands = 0;

	for (int i = 0; i < 7; i++)
	{
		int j,k,count;
		if (i < 4) {
			j = 0;
			k = i -j;
			count = k;
			while (j<=count)
			{
				if (b[j][k] == 1) {
					if (!check1(j, k))
					{
						cout << j << " " << k << endl;
						islands++;
					}
				}
				j++;
				k = i - j;
			}
		}
		else if(i>=4)
		{
			k = 3;
			j = i - k;
			count = j;
			while (k>=count)
			{
				if (b[j][k] == 1) {
					if (!check1(j, k))
					{
						cout << j << " " << k << endl;
						islands++;
					}
				}
				j++;
				k= i - j;
			}
		}
	}

	cout << islands;
    return 0;
}

- raghu.aok May 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int getTotalIslands(int[][] plane) {
		if (plane == null)
			return 0;
		int result = 0;
		for (int i = 0; i < plane.length; i++) {
			for (int j = 0; j < plane[0].length; j++) {
				if (plane[i][j] == 1) {
					result++;
					markTheIsland(plane, i, j);
				}
			}
		}
		return result;
	}

	public static void markTheIsland(int[][] plane, int i, int j) {
		if (plane[i][j] == 1) {
			plane[i][j] = -1;
			if ((j + 1) < plane[0].length)
				markTheIsland(plane, i, j + 1);
			if ((j - 1) >= 0)
				markTheIsland(plane, i, j - 1);
			if ((i + 1) < plane.length)
				markTheIsland(plane, i + 1, j);
			if ((i - 1) >= 0)
				markTheIsland(plane, i - 1, j);
		} else
			return;

	}

- Ankita Dahad January 18, 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