Sapient Corporation Interview Question for Web Developers


Country: India
Interview Type: Written Test




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

Simple to find the disjoint sets present in a graph.

- Damodar July 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FriendZone {

	public static void main(String[] args) {
		boolean[][] input = {{true,true,false},{true,true,true},{false,true,true}};
		System.out.println(find(input));
	}
	
	public static int find(boolean[][] input){
		int length = input.length;
		List<Set> list = new ArrayList<Set>();
		int totalNumberOfSets = length;
		for(int i=0;i<length;i++){
			HashSet<Integer> set = new HashSet<>();
			set.add(i);
			list.add(set);
		}
		for(int i=0;i<length;i++){
			for(int j=i+1;j<length;j++){
				if(input[i][j]){
					Set set0 = list.get(i);
					Set set1 = list.get(j);
					set0.addAll(set1);
					set1.clear();
					list.set(j, set0);
					totalNumberOfSets--;
				}
			}
		}
		return totalNumberOfSets;
	}

}

- dadakhalandhar August 05, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function solution(M){
let original = JSON.parse(M);
let result = [];
let done = [];
for(let i=0; i<original.length; i++){
if(done.indexOf(i) !== -1){
continue;
}

let row = original[i];

let newRow = [];
for(let j =0; j<row.length; j++){

if(row[j] || newRow,indexOf(j) !== -1){

mergeFriends(newRow.original[j])
done.push(j);
}
}
result[i] = newRow;

}


let output = '';
for(let i=0; i<result.length; i++){
let row = result[i];
if(row){
for(let j=0; j<row.length; j++){
output += row[j]+',';
}
output = output.substring(0,output.length-1);
output += '|';




}

}

output = output.substring(0,output.length-1);
return output;
}

function mergeFriends(row,friends){
for(let i =0; i<friends.length; i++){
let friend = friends[i];
if(friend && row.indexOf(i)<= -1){
row.push(i);
}
return row;
}
}

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

var solution = function(M) {
M = JSON.parse(M);
if(M.length === 0) {
return 0;
}

let len = M.length;
let result = [];
let nestedRes = [];

for(let i = 0; i < len; i++) {
for(let j = 0; j < len; j++) {
if(M[i][j] === 1) {
nestedRes.push(j);
M[i][j] = M[j][i] = '#';
}
}
if(!isSameSubset(result, nestedRes)) {
result.push(nestedRes);
}
nestedRes = [];
}
return result.join('|');
}

function isSameSubset(nestedArr, arr) {
if(nestedArr.length) {
for(var i=0; i<arr.length; i++) {
if(nestedArr.flat().indexOf(arr[i]) > -1){
return true;
}
}
} else {
return false;
}

}

solution("[[1,1,0],[1,1,0],[0,0,1]]")

- Anonymous June 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var solution = function(M) {
M = JSON.parse(M);
if(M.length === 0) {
return 0;
}

let len = M.length;
let result = [];
let nestedRes = [];

for(let i = 0; i < len; i++) {
for(let j = 0; j < len; j++) {
if(M[i][j] === 1) {
nestedRes.push(j);
M[i][j] = M[j][i] = '#';
}
}
if(!isSameSubset(result, nestedRes)) {
result.push(nestedRes);
}
nestedRes = [];
}
return result.join('|');
}

function isSameSubset(nestedArr, arr) {
if(nestedArr.length) {
for(var i=0; i<arr.length; i++) {
if(nestedArr.flat().indexOf(arr[i]) > -1){
return true;
}
}
} else {
return false;
}

}

solution("[[1,1,0],[1,1,0],[0,0,1]]")

- yogeshcs0107 June 16, 2019 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var solution = function(M) {
    M = JSON.parse(M);
    if(M.length === 0) {
        return 0;
    }
    
    let len = M.length;
    let result = [];
    let nestedRes = [];
    
    for(let i = 0; i < len; i++) {
        for(let j = 0; j < len; j++) {
            if(M[i][j] === 1) {
                nestedRes.push(j);
                M[i][j] = M[j][i] = '#';
            }
        }
        if(!isSameSubset(result, nestedRes)) {
             result.push(nestedRes);
        }
        nestedRes = [];
    }
    return result.join('|');
}

function isSameSubset(nestedArr, arr) {
    if(nestedArr.length) {
      for(var i=0; i<arr.length; i++) {
            if(nestedArr.flat().indexOf(arr[i]) > -1){
                return true;
            }
        }  
    } else {
        return false;
    }
    
}

solution("[[1,1,0],[1,1,0],[0,0,1]]")

- yogeshcs0107 June 16, 2019 | 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