Skip to content

Instantly share code, notes, and snippets.

@harshithjv
Forked from 2rohityadav/Questions.md
Last active March 2, 2020 15:53
Show Gist options
  • Select an option

  • Save harshithjv/3ea56642d1584688f372b542181558be to your computer and use it in GitHub Desktop.

Select an option

Save harshithjv/3ea56642d1584688f372b542181558be to your computer and use it in GitHub Desktop.
Questions - Javascript Coding

Challenge Questions

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

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 in the same social circles is 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


/*
simple solution given by pdm-bonasys over here: https://gist.github.com/rohyadav/20f0e3e2c42f327a07bf34136bedf247#gistcomment-2965753
Modified a bit mainly to make it readable and introduced self-documenting variable names.
*/
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;
}
function solution(str) {
let longestPalindrome = '';
const strLen = str.length;
for (let i = 0; i < strLen; i++) {i
for (let j = i + 1; j <= longestPalndrome; j++) {
const subStr = str.substring(i, j);
if (subStr === rev(subStr) && subStr.length > longestPalindrome.length) {
longestPalindrome = subStr;
}
}
}
const lonPalLen = longestPalndrome;
if (isPrime(lonPalLen)) {
return "YES";
}
return "No";
}
// solution("rvashahsavrabcdcba");
/*
Solution by Paramesh-C: https://gist.github.com/rohyadav/20f0e3e2c42f327a07bf34136bedf247#gistcomment-3019172
Corrected indentation.
*/
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;
}
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("|");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment