0% found this document useful (0 votes)
0 views

Programming_Questions

The document contains JavaScript functions for various algorithmic problems including finding two sums, adding two numbers represented as linked lists, calculating the length of the longest substring without repeating characters, finding the median of two sorted arrays, converting Roman numerals to integers, calculating the product of an array except itself, and determining the maximum profit from stock prices. Each function is defined with clear logic and examples demonstrating their usage. The document serves as a reference for solving common coding challenges.

Uploaded by

labdhi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Programming_Questions

The document contains JavaScript functions for various algorithmic problems including finding two sums, adding two numbers represented as linked lists, calculating the length of the longest substring without repeating characters, finding the median of two sorted arrays, converting Roman numerals to integers, calculating the product of an array except itself, and determining the maximum profit from stock prices. Each function is defined with clear logic and examples demonstrating their usage. The document serves as a reference for solving common coding challenges.

Uploaded by

labdhi07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Facebook

2 Sum

function twoSums(arr,target){
let maps = new Map();
for(let i=0; i<arr.length; i++){
let compl = target - arr[i];
if(maps.has(compl)){
return [i, maps.get(compl)];
}
maps.set(arr[i],i);
}
}

let arr = [2,7,11,15];


let res = twoSums(arr,9);
console.log(res);
arr = [3,2,4]
res = twoSums(arr,6);
console.log(res);
arr = [3,3]
res = twoSums(arr,6);
console.log(res);

Add 2 numbers

class Node{
constructor(val,next=null){
this.val = val;
this.next = next;
}

function addTwoNumbers(l1,l2){
let sum = 0;
let carry = 0;
let l3 = new Node(0);
let current = l3;

while(l1 != null || l2 != null || sum > 0){


if(l1 != null){
sum += l1.val;
l1 = l1.next;
}
if(l2 != null){
sum += l2.val;
l2 = l2.next;
}

if(sum > 9){


carry = 1;
sum = sum - 10;
}

current.next = new Node(sum);


current = current.next;
sum = carry;
carry = 0;

return l3.next();
}

Longest SubString

function repeatingChar(s){
if(s.length == 0) return 0;
if(s.length == 1) return s;

let max = 0;
let currChar = new Map();
for(let i=0, j=0; i<s.length; i++){
if(currChar.has(s[i])){
j = Math.max(currChar.get(s[i])+1,i);
}
max = Math.max(max, i-j+1);
currChar.set(s[i],i);
}
return max;

Median of 2 sorted array

var findMedianSortedArrays = function(nums1, nums2) {


if(nums1.length == 0 && nums2.length == 0){
return 0;
}

let merged = [...nums1, ...nums2];

merged.sort((a,b)=> a-b);

if(merged.length === 1){


return merged[0];
} else if(merged.length % 2 !== 0){
return merged[Math.floor(merged.length / 2)];
} else {
let left = merged[merged.length/2];
let right = merged[(merged.length/2)-1];
let res = (left+right)/2;
return res;
}
};

Roman to integer
var romanToInt = function(s) {
let map = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}
let res = 0;

for(let i=0; i< s.length; i++){


let cur = map[s[i]];
let next = map[s[i+1]];

if(cur < next){


res += next -cur;
i++;
} else {
res += cur;
}
}

return res;
};
Product Except Self

var productExceptSelf = function(nums) {


let prod = 1;

let res = [];

for(let i=0; i< nums.length; i++){


res[i] = prod;
prod = prod * nums[i];
}

prod = 1;

for(let j= nums.length-1; j>=0; j--){


res[j] = res[j] * prod;
prod = prod * nums[j];

return res;
};

Buy Sell Stock

var maxProfit = function(prices) {


let max = 0;
let min = Infinity;

for(let i=0; i<prices.length; i++){


if(prices[i] < min){
min = prices[i];
} else if(max < prices[i] - min){
max = prices[i] - min;
}

return max;
};

Zig Zag Conversion

You might also like