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. …
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:
Reverse a singly linked list.
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. …
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 everyone
Output: Hi EveryoneExample 2:Input: hello class
Output: Hello Class
There are several possible solutions to this problem such as using a for loop,
.map() method, or
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
n elements and a
target value, write a function to search
target exists, then return its index, otherwise return
Example 1:Input: nums = [-1,0,3,5,9,12], target = 9
Explanation: 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…
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 =…
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. …
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 20
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 = 9
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. …
Example 1:Input: (5)
Output: falseExample 2:Input: (82)
Output: trueExample 3:Input: (-1)
In this exercise, we will not use the modulo/remainder operator (