Author: Kavit

Closures, currying, partially applied function interview question in javascript

Write a function say which logs the following. console.log( say(‘Foo’)(‘bar’)() ) // => “Foo bar ” console.log( say(‘Hi’)(‘my’)(‘name’)(‘is’)(‘Foo’)() ) // “Hi my name is Foo ” Understanding closures, currying, partially applied functions in javascript...

increment an integer array by one

Problem Given an integer array, increment its numeric value by one. For example, increment([2, 7, 3]) returns [2, 7, 4] Solution

Find target subtraction is possible in a given unsorted array of numbers

Problem statment Given an array of unsorted numbers and a target element. Find if any two elements subtract to the target element or not. You can loop through the array only once. Constraint: Time...

Solution

Tic Tac Toe Winner

Interview Question Implement an algorithm that determines if someone has won a game of tic-tac-toe Algorithm To determine if any of the two players have won the TicTacToe game or not, We need to...

How to get the number from the middle of a string

Given the following string extract the number ‘Buy for \$199.99’ // 199.99 ‘Gas Price \$2.304 per gallon’ // 2.304 We can solve this using slice and, parseFloat(). slice returns the string from the starting...

hex to rgba conversion using javascript

Algorithm get two characters from the input string at a time and convert it to the integer For example let hex = ‘ff00ff’; let r = parseInt(hex.substring(0, 2), 16) // 255 concat all the...

Format the given number into an American currency format

Convert a given number into the formatted number. Logic Traverse the string in the reverse order Consider the following cases : Positive numbers, Negative numbers, Positive floating point numbers, Negative floating point numbers   Solution

Implement the function that determines whether you can or cannot seat a number of people in the row.

— Problem People are sitting in a theater row and you’re an usher. Given a row of seats that either occupied (1) or unoccupied (0) and the condition that new people being seated...

Connect nodes at the same level in a binary tree

PSEUDO ALGORITHM (Breadth first search approach) We will use the same algorithm discussed in level-order-breadth-first-search-or-zig-zag-traversal-of-a-binary-tree with slight modification. Create an empty queue q Initialize roots height = 0; Enqueue q with the root node Loop...