All content related to single-loop

Find Leaders in an Array

Given an integer array X[] of size n, write a program to find all the leaders in the array X[]. An element is a leader if it is strictly greater than all the elements to its right side.

Max Consecutive Ones

An input binary array X[] is given where all the elements are either 0 or 1. Write a program to find the maximum consecutive ones in this binary array.

Product of Array Except Self

Given an array X[] of n integers, write a program to return an array product[] such that product[i] is equal to the product of all the elements of X except X[i].

Fizz Buzz Problem

This game is played in a group, where players take turns to say the next number in a sequence, counting one at a time. If the number is divisible by 3, the player must say, “Fizz” instead of the number itself. If the number is divisible by 5, the player must say, “Buzz”. If the number is divisible by both three and five, the player has to say, “Fizz buzz”. Given a number n, write a program that outputs the string representation of numbers from 1 to n.

Number of buildings facing the sun

Given an input array height[] which represents the heights of buildings, write a program to count the number of buildings facing the sunset. It is assumed that the heights of all buildings are distinct.

Swap List Nodes in Pairs

Given a singly linked list, write a program to swap every two adjacent nodes and return its head. If the number of nodes are odd, then we need to pair-wise swap all the elements except the last element.

Count distinct elements in every window

Given array X[] of n integers, write a program to return the count of distinct numbers in all windows of size k.

Find next greater elements in an array

Given an array, find the next greater element for every element in the array. The next greater element for an element is the first greater element on the right side of the array. This is one of the best problems to learn problem-solving using stack.

Roman to Integer

Given a Roman numeral, write a program to find its corresponding decimal value. Roman numerals are represented by seven different symbols: I , V, X, L, C, D and M.

Reversing a Linked List

Write a program to reverse a linked list. A head pointer of a linked list is given and our task to reverse the entire list so that when the resulted list is traversed it looks like we are traversing the original list from tail to head.

The maximum difference between two elements

Given an array A[] of integers, find out the maximum difference between any two elements such that the larger element appears after, the smaller element. In other words, we need to find max(A[j] - A[i]), where A[j] > A[i] and j > i.

Longest Consecutive Sequence

Given an array X[] of n integers, write a program to find the length of the longest consecutive elements sequence. In other words, we need to find the length of the longest sub-sequence such that elements in the subsequence are consecutive integers. The consecutive numbers can be in any order.

Majority element in an array

You are given an array X[] consisting of n elements, write a program to find majority element in an array i..e return the number which appears more than n/2 times. You may assume that the array is non-empty and the majority element always exists in the array. A majority element is an element that appears more than n/2 times, so there is at most one such element.

Maximum Subarray Sum

Given an array X[] with n elements, we need to write a program to find the largest contiguous subarray sum. A subarray of array X[] of length n is a contiguous segment from X[i] through X[j] where 0<= i <= j <= n. Kadane algorithm idea is intuitive, using a single loop and few variables to solve the problem. We can use a similar idea to solve other coding problems.

n Repeated element in 2n size array

In an array of size 2n, there are n+1 unique elements, and exactly one of these elements is repeated n times. Return the element repeated n times.

Remove duplicates from sorted array

Write a program to remove the duplicates from the sorted array. For this we are given a sorted array, the task is to remove the duplicate elements such that there is a single occurrence of each element in the array.

Sort an array of 0s, 1s, and 2s —Dutch National Flag Problem

Given an array a[] consisting of 0s, 1s, and 2s. Write a program to sort the array of 0’s, 1’s, and 2’s in ascending order. This is a famous coding interview problem asked in facebook, microsoft and amazon.

Valid Mountain Array

Given an array X[] of n integers, return true if and only if it is a valid mountain array. The array X[] is a mountain array if and only if n >= 3 and there exists some i with 0 < i < n - 1 such that: X[0] < X[1] <...X[i-1] < X[i] and X[i] > X[i+1] > ...> X[n - 1]. In other words, we call the array mountain array when the array is strictly increasing and then strictly decreasing.

Sort an array in a waveform

Given an array of integers, sort the array into a wave-like arrangement. In other words, An array A[0..n-1] is sorted in wave form if A[0] >= A[1] <= A[2] >= A[3] <= A[4] >= ….This problem has been asked during google coding interview.

Find the minimum and maximum value in an array

Given an array X[] of size n, we need to find the maximum and minimum element present in the array. This coding problem has been asked during facebook and microsoft interview.

Find equilibrium index of an array

Write a program to find the equilibrium index of an array. An array's equilibrium index is an index such that sum of elements at lower indexes equal to the sum of elements at higher indexes. This is an excellent coding question to learn — how to optimize space complexity and solve the problem using a single scan.

Our weekly newsletter

Subscribe to get free weekly content on data structure and algorithms, machine learning, system design, oops and math. enjoy learning!