Toy Problems

ZigZag Conversion


The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this

P   A   H   N
A P L S I I G
Y   I   R
And then read line by line: "PAHNAPLSIIGYIR"

Write the code that will take a string and make this conversion ...

Read More

Pascal's Triangle


Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. To build the triangle, start with "1" at the top, then continue placing numbers below it in a triangular pattern. Example ...

Read More

Hackerrank Solution: Computing the GCD


Given two integers, x and y, a recursive technique to find their GCD is the Euclidean Algorithm. The algorithm states that, for computing the GCD of two positive integers x and y, if x and y are equal, GCD(x, y) = x. Otherwise GCD(x, y) = GCD(x - y, y ...

Read More

Hackerrank Solution: Almost Sorted


Given an array with n elements, can you sort this array in ascending order using only one of the following operations? 1. Swap two elements. 2. Reverse one sub-segment. Input Format The first line contains a single integer, n, which indicates the size of the array. The next line contains ...

Read More

Hackerrank Solution: Simple Array Sum


Given an array of integers, can you find the sum of its elements? Input Format The first line contains an integer, N, denoting the size of the array. The second line contains N space-separated integers representing the array's elements. Output Format Print the sum of the array's elements ...

Read More