# JavaScript: Inserting a Node at the Tail of a Linked List

## The Solution to a Problem Picked From HackerRank

In the previous article, we explained how to insert a node at the head of a linked list, and in this one, we will use a challenge from HackerRank to learn how to insert a node at the tail of a singly linked list:

You are given the pointer to the head node of a linked list and an integer to add to the list. Create a new node with the given integer. Insert this node at the tail of the linked list and return the head node of the linked list formed after inserting this new node. …

# JavaScript: Inserting a Node at the Head of a Linked List

## Explained Solution to a HackerRank Problem

For today’s algorithm, we will insert a node at the head of a singly linked list. Here is the challenge that I picked from HackerRank:

Given a pointer to the head of a linked list, insert a new node before the head. The next value in the new node should point to head and the data value should be replaced with a given value. Return a reference to the new head of the list. The head pointer given may be null meaning that the initial list is empty.

The problem is straightforward, let’s move to the solution:

1. Initialize a node class. …

## A Common Interview Question Asked at Many Companies

This week, we will cover another popular technical interview question from LeetCode’s Top Interview Questions List; Reverse Linked List:

`Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULL`

This article will not give many details about linked lists, but here is a very quick refresher: Linked lists are data structures that represent a series of nodes where each node contains two pieces of information: the value of the node and a pointer/reference to the next node in the list. …

# JavaScript: Capitalizing the First Letter of Each Word in a Sentence

## Explained Solution to a Popular Interview Question

Last week, I was asked to solve one of the most common questions on a technical interview for a software engineer role and I wanted to share my solution with you in today’s article. The challenge was asking to write a program that capitalizes the first letter of every word in a given sentence. Here are a couple of examples:

`Example 1:Input: Hi everyoneOutput: Hi EveryoneExample 2:Input: hello classOutput: Hello Class`

There are several possible solutions to this problem such as using a for loop, `.map()` method, or `.replace()`

# JavaScript: Binary Search Algorithm

## Explained With a Problem From LeetCode

For today’s algorithm, I picked the binary search which is one of the most commonly asked topics in technical interviews. The problem named binary search from LeetCode will be used to explain this concept:

Given a sorted (in ascending order) integer array `nums` of `n` elements and a `target` value, write a function to search `target` in `nums`. If `target` exists, then return its index, otherwise return `-1`.

`Example 1:Input: nums = [-1,0,3,5,9,12], target = 9Output: 4Explanation: 9 exists in nums and its index is 4`

While linear searching checks every single element one at a time and works in a sorted or unsorted piece of data, binary searching eliminates half of the remaining elements at a time and works only on sorted data. The basic idea behind this technique is to find a certain element in a sorted sequence using a divide and conquer approach. This algorithm allows us to quickly find the position of a target value within a sorted list basically by reducing the search area by half at each iteration and it is usually faster than linear search for large amounts of data. We will use iterative binary search technique to solve the given question and here is a visual representation of our…

# JavaScript: Remove Duplicates From Sorted Array in Place

## Challenge Picked From LeetCode’s Top Interview Questions List

Today, we will work on Remove Duplicates from Sorted Array algorithm from LeetCode’s Top Interview Questions List:

Given a sorted array nums, remove the duplicates in-place such that each element appears only once and returns the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

`Example 1:Given nums = [1,1,2],Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.It doesn't matter what you leave beyond the returned length.Example 2:Given nums =…`

# JavaScript: Valid Anagram Challenge

## Top Interview Questions List — LeetCode

In today’s blog post, I will build an algorithm that validates given inputs as anagrams. Here is one of the most classic interview challenge named Valid Anagram which I picked from LeetCode’s Top Interview Questions List:

Given two strings s and t, write a function to determine if t is an anagram of s (You may assume the string contains only lowercase alphabets).

Words that are anagrams are formed from another by rearranging its letters, so they will have the same letters with the same frequency in a different order. “listen” and “silent” can be given as an example to a common anagram. For this challenge, we need to determine if given strings are valid anagrams of each other. …

# JavaScript: How to Solve The Sock Merchant Problem

## A Challenge from HackerRank’s Interview Preparation Kit

For this week’s algorithm, I picked a problem named Sock Merchant from HackerRank. I slightly adjusted the challenge as we will not be using a second parameter for the number of socks:

Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

The problem states that we are given an array of integers, where each number represents a distinct color of socks. We will need to write a function that returns the total number of matching pairs of socks that exists in the list.

`Example:Input: 10 20 20 10 10 30 50 10 20Output…`

# JavaScript: The Two Sum Problem

## From LeetCode’s Top Interview Questions List

This week’s algorithm; Two Sum, is picked from LeetCode’s Top Interview Questions list:

Given an array of integers `nums` and an integer `target`, return indices of the two numbers such that they add up to `target`. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Check this example out to better understand what the problem is asking:

`Example:Input: nums = [2,7,11,15], target = 9Output: [0,1]Output: Because nums + nums == 9, we return [0, 1].`

Array type of questions like this problem is frequently asked in technical interviews and it has many different ways to solve it. The simple brute force solution is to scan each index pair and their sum of a given array with nested loops. This inefficient solution requires a time complexity of O(n²) with two nested for loops since we first loop through each element of the array and then loop through each element again to return their indices if they add up to the target number. …

# JavaScript: Use a Recursive Function to Determine If a Given Number is Even

## Explained the Solution of an Exercise in Eloquent JavaScript

I picked an exercise called “recursion” from the third chapter of the Eloquent Javascript book for this week’s algorithm (3rd edition, chapter 3; Functions). We will write a recursive function that takes a whole number as its single parameter and defines whether the given number is even or odd by returning a boolean value. Here are a couple of examples:

`Example 1:Input: (5)Output: falseExample 2:Input: (82)Output: trueExample 3:Input: (-1)Output: false`

In this exercise, we will not use the modulo/remainder operator (`%`). … 