Skip to content

Instantly share code, notes, and snippets.

@2rohityadav
Last active March 2, 2020 07:51
Show Gist options
  • Save 2rohityadav/20f0e3e2c42f327a07bf34136bedf247 to your computer and use it in GitHub Desktop.
Save 2rohityadav/20f0e3e2c42f327a07bf34136bedf247 to your computer and use it in GitHub Desktop.
Questions - Javascript Coding

ChallengeQuestions

Lucky Palindrome / Social Circle

QUESTION 1

Lucky Palindrome

QUESTION DESCRIPTION

A String is called a palindrome if it can be read the same way in either direction For example, "malayalam" is a palindrome, but "tamil" is not

A "lucky palindrome" is a palindrome of prime length,

Now, you task is to find out the length (denoted by L) of the longest palindrome sub-sting (maximum-length contiguous substring of a given sting that is also a palindrome) and print "YES" if it is a prime number, otherwise print "NO"

Write a function

function solution(s);

that given an string S, returns "YES" / "NO" ) based on whether the input string's longest palindrome substring is a prime number.

For example, given a string

S = "122243323341"

the function should return "YES" (quotes only for clearity) because the longest palindrome substring in S is "4332334", the length is 7 and 7 is a prime number

Assumptions

The string only consists of english alphabets A-Z(uppercase or lowercase) and digits 0-9


QUESTION 2

Social Circle

QUESTION-DESCRIPTION

A String is called a palindrome if it can be read the same way in either direction For example, "malayalam" is a palindrome, but "tamil" is not

There are S students in a class. Some of them are fiends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B and B is a direct friend of C, then A is an indirect friend of C.

A "social circle" is a group of students who are direct or indirect friends.

You are given a S*S matrix M (S is the number of students) representing the friend relationship between students in the class. if M[i][j] = 1, then the ith and jth students are direct friend with eachother, otherwise not.

Your task is to return the string, representing all the social circles separated by pipe(i) among all the students. Individual members within circle should be seprated by a comma(,)

Write a function

function solution(s);

that given a zero-indexed double-dimensional array A of dimensions S*S (in string format to be converted to a 2-dimensional array), returns a string representing pipe-separated (|) social circles of comma-separated friends.

The function should return "0,1|2" (qutoes for representation purpose only)

The 0th and 1st students are direct friends (M[0][1]) = 1 and subsequently M[1][0] = 1), so they are in a social circle.

The 2nd student is in her/his own social circle. So return 2.

For another example, given:

M = "[
  [1,1,0],
  [1,1,1],
  [0,1,1]
]"

the function should return "0,1,2"

The 0th and 1st students are direct friends, the 1st and 2nd students are direct friends. Thus, the 0th and 2nd are indirect friends. All of them are ub tge sine sociak curckes si retyrb 1.

You can assume that:

  • for every i within the range [0.. N - 1] M[i][i] = 1

In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment


@slgauravsharma
Copy link

please add answers also.

@pdm-bonasys
Copy link

Question 1 Answer

function solution(str) {
let lp='';
for(let i=0;i<str.length;i++){
for(let j=i+1;j<=str.length;j++){
const sbtr = str.substring(i,j);
if(sbtr === rev(sbtr) && sbtr.length>lp.length){
lp = sbtr;
}

    }    
}
if(isprime(lp.length)){
    console.log("YES"+" Longest palindrome string is :"+lp+' and Length:'+lp.length)
}else{
    console.log("No"+" Longest palindrome string is :"+lp+' and Length:'+lp.length)
}

}
function rev(str){
return str.split('').reverse().join('');
}
function isprime(no){
if(no <=1){
return false;
}
if(no == 2){
return true;
}
for(let i=2;i<no;i++) {
if(no%i===0){
return false;
}
}
return true;
}
solution("rvashahsavrabcdcba");

@chandrapratap90
Copy link

Does anyone has the solution for the 2nd Question ? Please share. It would be helpful.
Thanks !

@Paramesh-C
Copy link

`var findCircleNum = function(M) {
let visited = new Array(M.length).fill(false), cnt = 0, socialCircle = [];
for (let i = 0; i < M.length; i++) {
if (visited[i] == false) {
visited[i] = true
socialCircle[cnt] = "";
socialCircle[cnt] = helper(M, visited, i, socialCircle[cnt])
cnt++
}
}
return socialCircle.join("|");
};

var helper = function(mat, visited, i, socialCircle) {
socialCircle = socialCircle + ((socialCircle.indexOf(i) == -1) ? (socialCircle == "" ? "" : ",") + i : "");
for (let j = 0; j < mat.length; j++) {
if (mat[i][j] == 1 && visited[j] == false) {
visited[j] = true
socialCircle = helper(mat, visited, j, socialCircle)
}
}
return socialCircle;
}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment