Problem: Jumping On The Clouds

Q. There is a new mobile game that starts with consecutively numbered clouds. Some of the clouds are thunderheads and others are cumulus. The player can jump on any cumulus cloud having a number that is equal to the number of the current cloud plus 1 or 2. The player must avoid the thunderheads.

Determine the minimum number of jumps it will take to jump from the starting position to the last cloud. It is always possible to win the game.

For each game, you will get an array of clouds numbered 0 if they are safe or 1 if…

Photo by Paola Galimberti on Unsplash

Problem: Repeated String

Q. There is a string, “s”, of lowercase English letters that is repeated infinitely many times. Given an integer, “n”, find and print the number of letter “a’s” in the first n letters of the infinite string.

long repeatedString(String s, long n);

  • s: a string to repeat
  • n: the number of characters to consider
  • returns long: the frequency of “a’s” in the substring


Input: s = “abcac”, n = 10
Output: 4
Explanation: With this input, the substring we need to consider is “abcacabcac”, the first 10 characters of the infinite string. …

Problem: Remove Duplicates From The Sorted Array

Q. Given a sorted array “nums”, remove duplicates from the sorted array, 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.


Input: nums = [1,1,2]
Output: 2, nums = [1,2]
Explanation: Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

Input: nums = [0,0,1,1,1,2,2,3,3,4] Output: 5, nums = [0,1,2,3,4] Explanation: Your function should return length = 5, with the first five elements of nums…

HackerRank: Counting Valleys
HackerRank: Counting Valleys
Photo by Jad Limcaco on Unsplash

Problem: Counting Valleys

Q. An avid hiker keeps meticulous records of their hikes. During the last hike that took exactly S steps, for every step, it was noted if it was an uphill, U, or a downhill, D step. Hikes always start and end at sea level, and each step up or down represents a 1 unit change in altitude. We define the following terms:

  • A mountain is a sequence of consecutive steps above sea level, starting with a step up from sea level and ending with a step down to sea level.
  • A valley is a sequence of consecutive steps below sea…

Problem: 2D Array

Q. Given a 6×6 2D Array, arr:

1 1 1 0 0 0
0 1 0 0 0 0
1 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

An hourglass in arr is a subset of values with indices falling in this pattern in arr’s graphical representation:

a b c
e f g

There are 16 hourglasses in arr of 6×6 grid. An hourglass sum is the sum of an hourglass’s values. …

Problem: Sales By Match

Q. There is a large pile of socks that must be paired by color. Given an array of integers representing the color of each sock, determine how many pairs of socks with matching colors there are.

Code the sockMerchant method:

public int sockMerchant(int n, int[] ar);

  • int n: the number of socks in the pile
  • int ar[n]: the colors of each sock
  • Returns int: the number of pairs


Input: n = 7, ar = [1, 2, 1, 2, 1, 3, 2]
Output: 2

Input: n = 9, ar = [10, 20, 20, 10, 10, 30, 50, 10, 20]
Output: 3

Problem: Left Rotation

Q. A left rotation operation on an array shifts each of the array’s elements 1 unit to the left. For example, if 2 left rotations are performed on array [1,2,3,4,5], then the array would become [3,4,5,1,2].

Note: The lowest index item moves to the highest index in a rotation. This is called a circular array.

Given an array “a” of “n” integers and a number, “d”, perform “d” left rotations on the array. Return the rotated array.

int[] rotLeft(int[] a, int d)

  • int a[n]: the array to rotate
  • int d: the number of rotations
  • returns int a’[n]: the rotated array


Problem: Kth Largest Element In An Array

Q. Given an integer array “nums” and an integer “k”, return the “kth” largest element in the array.

Note: It is the kth largest element in the sorted order, not the kth distinct element.


Input: nums = [3,2,1,5,6,4], k = 2
Output: 5

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4

Algorithm: Sorting

The naive solution is to sort the input array first and then return the kth element from the end.

  • Time complexity: O(n log n)
  • Space complexity: O(1)
class Solution { public int findKthLargest(int[] nums, int k) { if (nums.length == 0 || k > nums.length) { return -1…

Vishwajeet Singh

Java and ADF developer for 12+ years. Love Java , WordPress , SQL, PLSQL and related technologies. Open source enthusiast.

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