The Solution to a Problem Picked From HackerRank

Image for post
Image for post

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. …


Explained Solution to a HackerRank Problem

Image for post
Image for post

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

Image for post
Image for post
Photo by JJ Ying on Unsplash

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

Reverse a singly linked list.

Example:Input: 1->2->3->4->5->NULL
Output: 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. …


Explained Solution to a Popular Interview Question

Image for post
Image for post
Image by PIRO4D from Pixabay

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 Everyone
Example 2:Input: hello class
Output: Hello Class

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


Explained With a Problem From LeetCode

Image for post
Image for post
Image by StockSnap from Pixabay

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 = 9
Output: 4
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…


Challenge Picked From LeetCode’s Top Interview Questions List

Image for post
Image for post
Photo by Jørgen Håland on Unsplash

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 =…


Top Interview Questions List — LeetCode

Image for post
Image for post
Image by Pexels from Pixabay

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. …


A Challenge from HackerRank’s Interview Preparation Kit

Image for post
Image for post
Image by anastacia bankulova from Pixabay

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 20
Output…


From LeetCode’s Top Interview Questions List

Image for post
Image for post
Photo by Bekky Bekks on Unsplash

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 = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 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. …


Explained the Solution of an Exercise in Eloquent JavaScript

Image for post
Image for post
Image by Tuan Hung Nguyen from Pixabay

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: false
Example 2:Input: (82)
Output: true
Example 3:Input: (-1)
Output: false

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

About

Bahay Gulle Bilgi

Full Stack Software Developer with a background in academia. Seattle, WA. https://www.linkedin.com/in/bahay-gulle-bilgi/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store