JavaScript LIVE Coding Interview Round (Mock)

JavaScript LIVE Coding Interview Round (Mock)

Coder Dost

1 год назад

39,066 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@shazaibahmad7807
@shazaibahmad7807 - 05.02.2024 13:06

Bro simple is that one if condition if(arr[I+1]-arr[I]===4)
{
return are[I+1]+2;
}
else{
return;
}

Ответить
@abdulbasith650
@abdulbasith650 - 31.01.2024 20:49

let arr = [1,3,5,7,9,13]

function misodd(arr){
let misnum;
for(let i=0;i<arr.length;i++){
if(arr[i]+2!=arr[i+1]){
return arr[i]+2;
}
}
return -1;
}
let ans=misodd(arr)
console.log(ans)

Ответить
@a.spragadeeshbalaji2917
@a.spragadeeshbalaji2917 - 28.01.2024 14:56

//FIND THE MISSING ODD NUMBER WITHIN THE RANGE SPECIFIED IN THE ARRAY
const input = [5,7,9,11,15,17];

for(let i=input[0]; i<=input[input.length-1]; i++){
if((i % 2 == 1) && (!input.includes(i))){
console.log(i);// OUTPUT - 13
}
}

Ответить
@rajatgour9686
@rajatgour9686 - 25.12.2023 16:20

ok so let assume given arry is sorted
const input = [5,7,9,11,15,17]

let output = null
for(let start = input[0]+2;start<=input[input.length-1];start+=2){
if(!input.includes(start)){
output = start
break;
}
}

Ответить
@ankitarya7492
@ankitarya7492 - 21.12.2023 14:35

let arr = [1, 5, 7, 9, 11,13];
let j = arr[0];
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== j) {
console.log(j)
break
}
j+= 2
}

KeepItSimple

Ответить
@user-oe3ld5kw6d
@user-oe3ld5kw6d - 10.12.2023 09:11

This does work fine!
const inp = [5,7,9,11,15,17];
const outp = [];

for(let i=0; i < inp.length; i++){
if(inp[i]+2 != inp[i+1]){
outp.push(inp[i]+2);
}
}

console.log(outp);

Ответить
@n_fan329
@n_fan329 - 08.12.2023 08:18

let tab=[5,7,9,11,15,17]
let fullODD=[]
let missing
let min= Math.min(...tab)
let max= Math.max(...tab)

for (let i=min;i<=max;i++) {
i%2!==0 && fullODD.push(i)
}

for (let i=0; i<fullODD.length;i++) {
!tab.includes(fullODD[i]) ? missing=fullODD[i] : null
}

console.log(missing) // 13

Ответить
@ankursharma1822
@ankursharma1822 - 03.12.2023 20:33

let arr = [5,7,9,11,15,17];
for(value of arr){
let a = value +2;
if(!arr.includes(a)){
console.log(a);
break
}
}

Ответить
@bathininaveen5198
@bathininaveen5198 - 26.11.2023 08:55

function findMissingOdd(arr){
for(let i =0;i<arr.length;i++){
if(arr[i+1]-arr[i] > 2) return arr[i] + 2;
}
return "No element is Missing!"
}

Ответить
@ankittyagi6706
@ankittyagi6706 - 06.11.2023 02:28

const input =[5,7,9,11,15,17];



const findMissingOdd =(low,high)=>{
const arr=[];
for(let i=low;i<=high;i++){
if(i%2 !==0){
arr.push(i)
}
}
return arr.find((ele)=>!input.includes(ele));
}

console.log(findMissingOdd(input[0],input[input.length-1]))

Ответить
@Rubyd777
@Rubyd777 - 07.10.2023 13:23

I guess using some mathematics can be helpful.
But i would use it only if array was large and we need to solve it in minimum time complexity else i would use sorting approach

1) Find max and min at once from array
2)find sum of odd numbers from 1 to nth term(let x)
3)find total sum of elemnts given in array (let y)
4)find total sum of odd number from min to max that we found above(let z)
5)lastly: (x-y)-(x-z)
This can be simplified but you compolsary need x to find y and z

Please correct if i am wrong.
I just ried my approach

Ответить
@user-pj9ny2vt2l
@user-pj9ny2vt2l - 06.10.2023 19:42

easy first sort then extract min and max after than loop range from min to max than extrat odd number list that number not in arr that is missing after first missing get break the array

Ответить
@abnow1998
@abnow1998 - 03.10.2023 21:37

It is in AP(Arithmetic Progression)
Just check Common Difference.
Formula: a+(n-1)*d
a is first number, d is common diff

Ответить
@FarhanKhan-ox4iy
@FarhanKhan-ox4iy - 20.09.2023 22:10

I see in the comments no one has used two pointers for this. With that we can achieve linear time complexity. Use start and end. Run while loop until end >= start. start++ and end--

Ответить
@FreeTrial0315
@FreeTrial0315 - 19.09.2023 09:58

const arr =[5,7,9,11,15,17,19,23];
const data = new Set(arr)


for(i=5; i<=arr[arr.length-1]; i++){
if(i%2 !==0 ){
if(!data.has(i)){
console.log(i)
}
}
}

Ответить
@remonroy34
@remonroy34 - 17.09.2023 15:13

let value = [5, 7, 9, 11, 15];

let missingValue = [];
for (let i = 5; i < 15; i = i + 2) {
if (!value.includes(i)) {
missingValue.push(i);
}
}
console.log("🚀 ~ file: basic.js:4 ~ missingValue:", missingValue);

Ответить
@laylahashmi4234
@laylahashmi4234 - 14.09.2023 06:07

function missingOddNum(arr) {
let completeArr = [arr[0]];
for (let i = 0; i < arr.length; i++) {
let value = completeArr[i] + 2;
completeArr.push(value)
}
return completeArr.filter((num) => !arr.includes(num))
}

Ответить
@deveshmehra9643
@deveshmehra9643 - 10.09.2023 21:01

const input2 = [5, 7, 9, 11, 15, 17];


function findOddNumber2(input2) {
let minVal = Math.min(...input2);
let maxVal = Math.max(...input2);

// expected odd number
let expectedOddNumber = [];
for (let i = minVal; i <= maxVal; i++) {
if (i % 2 !== 0) {
expectedOddNumber.push(i);
}
}

// using linear search to find the missing odd number
for (let i = 0; i < input2.length; i++) { // or use forEach
if (input2[i] !== expectedOddNumber[i]) {
return expectedOddNumber[i];
}
}
}



console.log(findOddNumber2(input2));

Ответить
@happylaughy2777
@happylaughy2777 - 30.08.2023 17:53

const arr= [3,5,7,9,13]
let odd=arr[0];
for(let i=0;i<arr.length;i++){
if(arr[i]!=odd){
console.log (odd);
}
else{
odd+=2
}
}

Ответить
@adnaan28
@adnaan28 - 26.08.2023 18:34

const data = [5, 7, 11, 15, 17]


for(let i =data[0]; i <= data[data.length -1]; i ++ ) {
if( i % 2 !== 0){
if(!data.includes(i)) {
console.log(i)
}
}
}

Ответить
@an.ma007
@an.ma007 - 26.08.2023 00:54

const arr=[5,7,9,11, 15,17]

function findMiss(arr){
for(let i=0; i<arr.length; i++){
if( (arr[i+1]-arr[i]) !== 2){
return arr[i]+2
}
}
}

console.log(findMiss(arr))

Ответить
@unboxtheboxed-ox3cp
@unboxtheboxed-ox3cp - 24.08.2023 12:58

function oddcalc(arr){
for(let i=0;i<arr.length;i++){
if(Math.abs(arr[i]-arr[i+1])>2){
missing = (arr[i]+arr[i+1])/2;
return missing;
}
}
}

let val =oddcalc([5, 7, 9, 11, 13, 15, 19]);
console.log(val)

Ответить
@prakharsaxena7240
@prakharsaxena7240 - 23.08.2023 22:27

@coderDost Please let us know how to break forEach loop ? without using try-catch

Ответить
@prakharsaxena7240
@prakharsaxena7240 - 23.08.2023 22:26

for (let i=0; i<input.length-1; i++ ){
if(!input.includes(input[i]+2)) return input[i]+2;
}

Ответить
@nj-neeraj8985
@nj-neeraj8985 - 12.08.2023 04:56

//AP addition formula s= n/2(frst +last)
let len = input.length ;
return (input[0] +input[len-1])(len+1)/2 - input.reduce((a,c)=>a+c);

Ответить
@amazondeals7284
@amazondeals7284 - 06.08.2023 14:49

Solution 1:

const input = [5,7,9,11,15,17]

const findMissing =(arr)=>{
let temp = arr[0]
for(let i= 0; i< arr.length; i++){
if(temp !== arr[i]){
return temp;
}
temp += 2
}
}

console.log(findMissing(input))

Ответить
@shrikantjha4686
@shrikantjha4686 - 04.08.2023 09:02

let missingOdd = null;

for (let i = nums[0]; i <= nums[nums.length - 1]; i += 2) {
if (!nums.includes(i)) {
missingOdd = i;
break;
}
}

Ответить
@AbhishekSharma-fg1ho
@AbhishekSharma-fg1ho - 26.07.2023 22:36

its front end dev interview?? and which type of qus asking in front end dev

Ответить
@GurpreetSingh-gogsy
@GurpreetSingh-gogsy - 23.07.2023 19:35

const input = [5,7,9,11,15,19];
for(let i=1;i<input.length;i++){
if(input[i-1]+2 != input[i]){
console.log(input[i-1]+2)
break
}
}

Ответить
@payalkothari4862
@payalkothari4862 - 15.07.2023 15:14

let missingNumber;

for (let i = input[0]; i <= input[input.length - 1]; i += 2) {
if (!input.includes(i)) {
missingNumber = i;
break;
}
}

console.log("Missing number:", missingNumber);

Ответить
@mohaimin95
@mohaimin95 - 08.07.2023 17:00

function missingOdd(arr) {
for(let i = 1; i < arr.length; i++) {
const last = arr[i - 1];
const current = arr[i];
if(current - last > 2) {
return last + 2;
}
}
}

Ответить
@WalkingtotheTruth
@WalkingtotheTruth - 06.07.2023 17:51

Binary Search is possible here is the answer:

function findMissingOddNumber (arr) {
let left = 0
let right = arr.length - 1
let startingNumber = arr[0]

let ans = -1
while (left <= right) {
const mid = Math.floor((left + right) / 2)

let expectedNumber = startingNumber + 2 * mid

if (arr[mid] === expectedNumber) {
// Continue searching on the right side
left = mid + 1
} else {
// Continue searching on the left side
ans = expectedNumber
right = mid - 1
}
}

// If no missing number is found
return ans
}

Ответить
@akkburf5450
@akkburf5450 - 29.06.2023 23:20

let arr = [5,7,9,11,15,17]

function missNumber(arr){
for(let i =0 ; i < arr.length ; i++ ){
if(!(arr[i] + 2 === arr[i + 1])) return arr[i] + 2
}
}


console.log(missNumber(arr))

Ответить
@darklord9500
@darklord9500 - 28.06.2023 13:53

function findMissingNumber(arr) {
let missingNumber = null;
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== arr[0] + i * 2) {
missingNumber = arr[0] + i * 2;
break;
}
}
return missingNumber;
}

Ответить
@jaspreetsingh-5259
@jaspreetsingh-5259 - 27.06.2023 19:25

const arr=[5,7,9,11,15,17];

for (let i=arr[0];i<=arr[arr.length-1];i++){
if(i%2!==0){
if(!arr.includes(i)){
console.log(i);
}
}
}

Ответить
@yesuraju-tf1yp
@yesuraju-tf1yp - 09.06.2023 09:32

function missOdd(arr){
let first=0, last=arr.length-1, mid = 0 ,first_element = arr[0];

if(first_element+2*last == arr[last] ) return "no element is missed";

while(first<=last){
mid = Math.floor((first+last)/2);
if(arr[mid]==first_element+2*mid) first = mid+1;
else last = mid-1;
}

return first_element + 2*first;

}

console.log(missOdd([5,7,11,13,15,17]));

😀

Ответить
@kiranm5419
@kiranm5419 - 05.06.2023 21:06

const input = [3,5,7,9,11,15,17];

for(let i=0;i<input.length;i++){
if((input[i]+2) != input[i+1]){
console.log(input[i]+2);
break;
};
};

Ответить
@kiranm5419
@kiranm5419 - 05.06.2023 20:22

const input = [5,7,9,11,15,17];
const a = [];
const b = [];

for(let i=0;i<input.length;i++){
if(input[i] % 2 !== 0){
a.push(input[i]);
};
};

for(j=5; j<=17; j++){
if(j % 2 !== 0){
b.push(j);
};
};

for(i=0;i<a.length;i++){
if(a[i] !== b[i]){
console.log(b[i]);
break;
};
};

Ответить
@suryateja9881
@suryateja9881 - 03.06.2023 14:07

My solution for Sum 1:
let input = [21,23,25,27,31,33,35,37,39,41,45,47,49,53];
for (let i=input[0];i<=input[input.length-1];i++){
if((i%2 !== 0) && (!input.includes(i))){
console.log(i);
}
}

Ответить
@tonmaysardar3331
@tonmaysardar3331 - 02.06.2023 16:58

const input=[1,3,5,7,11,13,15,17]
let b;
for (let i=1;i<input.length-1;i++){
if (input[i+1]-input[i]!=input[i]-input[i-1]){
b=(input[i+1]+input[i])/2;
break;
}
}
console.log(b)

Ответить
@riyatiwari7178
@riyatiwari7178 - 30.05.2023 17:35

const input = [5, 7, 9, 11, 15, 17]
// 13
const missNums = []
function findMissingNum (input){

const minValue = Math.min(...input)
const maxValue = Math.max(...input)

for(let i=minValue; i<maxValue; i++){

if(i % 2 !== 0){

if(input.indexOf(i) === -1){
missNums.push(i)

}
}

}

return missNums

}

console.log(findMissingNum(input));

Ответить
@varandeepsahota1314
@varandeepsahota1314 - 29.05.2023 18:23

Binary search is POSSIBLE here.

We can determine which element should be present at ith index using simpe A.P formula and if correct element present, move right else move left side.

Thanks for this content.

Ответить
@harshjoshi6496
@harshjoshi6496 - 27.05.2023 22:55

@coderdost Binary search is possible here,
Solution: (Explanation at last)
const input = [5, 7, 9, 11, 15, 17,19, 21];
first = 0
last = input.length-1
mid = (first+last)/2

while (first<last){
if (input[mid]===input[0]+(mid*2)&&input[mid+1]!=input[0]+((mid+1)*2)){
mid=mid+1
console.log("Element missing at index ",mid)
console.log("Missing Element ",input[0]+(mid*2))
break
}
if (input[mid]!=input[0]+(mid*2)){
last=mid
mid = (first+last)/2

}
else{
first = mid
mid = ((first+last)/2)+1
}
mid=Math.round(mid)

}



Explanation:
In this question there is a correlation between the index and the element present on the index, the element in the N'th index should always be equal to input[0]+(N*2)

With the help of binary search, we can find the position at the array where the element at mid is the correct element according to this correlation, but the next element is not,
then the missing element can be calculated by putting N = mid+1 in this formula. Also in case of multiple missing numbers, this will always print the first missing number as we are searching for the last correct element according to this formula. If there are no missing numbers, then nothing will be printed.

If the array is an AP, then the approach will remain the same and we will just replace the formula with the one for that particular AP

Ответить
@AkashSingh-xf6bd
@AkashSingh-xf6bd - 27.05.2023 14:52

Coder Dost, Hi
I did not get you "n" concept in last
can you explain me

Ответить
@AkashSingh-xf6bd
@AkashSingh-xf6bd - 27.05.2023 14:42

let input =[5,7,11,13,15,17];
let out = 13
function s(input){
for(let i= 0; i<input.length; i++){
let temp = input[i]
if(temp+2 !== input[i+1])
{
return input[i]+2
}
}
}
// s(input)
console.log(s(input))

Ответить
@YourSecularBro
@YourSecularBro - 26.05.2023 23:20

my code works with any case:

const input = [1,2,3,4,5,6,7,9];
function checkMate(arr) {
let diff = arr[1] - arr[0];
let i = 0;
let j = arr.length-1;
let ans;
while (i < j) {
if(arr[i] + diff === arr[i+1]) {
i++;
} else {
ans = arr[i] + diff;
console.log(ans);
break;
}
}
}
checkMate(input);

Ответить
@prathamlashkari4230
@prathamlashkari4230 - 26.05.2023 20:37

let input = [5, 7, 9, 11,15, 17];
for (let i = 0; i < input.length; i++) {
let diff = input[i + 1]-input[i];
if (diff !== 2){
console.log(input[i]+2);
break;
}
}

Ответить
@zimbasardhara3450
@zimbasardhara3450 - 26.05.2023 05:38

let data =[1,3,5,7,9,11,15,17,19,21]
let data2=[];

for(let i=0;i<data.length-1;i++){
if(data[i]+2 !== data[i+1]){
data2.push(data[i]+2);
}
}
console.log(data2);

Ответить
@nikhilnikki9627
@nikhilnikki9627 - 23.05.2023 02:33

IS THIS CORRECT WAY:
const input=[5,7,9,11,15,17]
let first =input[0]
for (let i of input){
if (i ===first){
let next = first+2
first=next
}
else{
console.log(first)
}

}

Ответить
@paritoshchauhan3266
@paritoshchauhan3266 - 22.05.2023 10:00

Binary "search", name itself suggests you will be searching for something but since you don't know what to search for then obviously BS will not work here.

Ответить