Category Archives: Hackerrank

HackerRank Çözümleri – Mars Exploration

HackerRank içerisinde bulunan “Mars Exploration” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir “SOS” string’inin orijinalinden kaç adet farklı karakter barındırdığını bulmanız isteniyor.

► HackerRank – Mars Exploration: https://www.hackerrank.com/challenges/mars-exploration/problem

► Problem açıklaması:

A space explorer’s ship crashed on Mars! They send a series of SOS messages to Earth for help.

Letters in some of the SOS messages are altered by cosmic radiation during transmission. Given the signal received by Earth as a string, s, determine how many letters of the SOS message have been changed by radiation.

Example

s = “SOSTOT”

The original message was SOSSOS. Two of the message’s characters were changed in transit.

Function Description

Complete the marsExploration function in the editor below.

marsExploration has the following parameter(s):

string s: the string as received on Earth

Returns

int: the number of letters changed during transmission

Sample Input 0

SOSSPSSQSSOR

Sample Output 0

3

Explanation 0

= SOSSPSSQSSOR, and signal length s = |12|. They sent 4 SOS messages (i.e.: ).

Expected signal: SOSSOSSOSSOS

Recieved signal: SOSSPSSQSSOR

Difference: X X X

Sample Input 1

SOSSOT

Sample Output 1

1

Explanation 1

s = SOSSOT, and signal length 6. They sent 2 SOS messages (i.e.: ).

Expected Signal: SOSSOS

Received Signal: SOSSOT

Difference: X

Sample Input 2

SOSSOSSOS

Sample Output 2

0

Explanation 2

Since no character is altered, return 0

HackerRank Çözümleri – Super Reduced String

HackerRank içerisinde bulunan “Super Reduced String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string‘in içerisinde, yan yana olan aynı karakterlerin silinerek elde edilebilecek son string’i bulmanız isteniyor.

► HackerRank – Super Reduced String: https://www.hackerrank.com/challenges/reduced-string/problem

► Problem açıklaması:

Reduce a string of lowercase characters in range ascii[‘a’..’z’]by doing a series of operations. In each operation, select a pair of adjacent letters that match, and delete them.

Delete as many characters as possible using this method and return the resulting string. If the final string is empty, return Empty String

Example.

s = “aab”

aab shortens to b in one operation: remove the adjacent a characters.

s = “abba”

Remove the two ‘b’ characters leaving ‘aa’. Remove the two ‘a’ characters to leave ”. Return ‘Empty String’.

Function Description

Complete the superReducedString function in the editor below.

superReducedString has the following parameter(s):

string s: a string to reduce

Returns

string: the reduced string or Empty String

Sample Input 0

aaabccddd

Sample Output 0

abd

Explanation 0

Perform the following sequence of operations to get the final string:

aaabccddd → abccddd → abddd → abd

Sample Input 1

aa

Sample Output 1

Empty String

Explanation 1 aa → Empty String

Sample Input 2

baab

Sample Output 2

Empty String

Explanation 2

baab → bb → Empty String

HackerRank Çözümleri – Grading Students

HackerRank içerisinde bulunan “Grading Students” sorusunun açıklaması ve çözümü. Bu soruda size verilen sınav sonuçlarını, aşağıda yazılan kurallara göre yuvarlamanız isteniyor.

► HackerRank – Grading Students: https://www.hackerrank.com/challenges/grading/problem

► Problem açıklaması:

HackerLand University has the following grading policy:

– Every student receives a in the inclusive range from 0 to 100.

– Any grade less than 40 is a failing grade.

Sam is a professor at the university and likes to round each student’s according to these rules:

– If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.

– If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.

Examples

grade = 84 round to (85 – 84 is less than 3)

grade = 29 do not round (result is less than 40)

grade = 57 do not round (60 – 57 is 3 or higher)

Given the initial value of grade for each of Sam’s n students, write code to automate the rounding process.

Function Description

Complete the function gradingStudents in the editor below.

gradingStudents has the following parameter(s):

int grades[n]: the grades before rounding

Returns

int[n]: the grades after rounding as appropriate

Sample Input 0

4

73

67

38

33

Sample Output 0

75

67

40

33

HackerRank Çözümleri – CamelCase

HackerRank içerisinde bulunan “CamelCase” sorusunun açıklaması ve çözümü. Bu soruda size camelCase olarak yazılmış bir string’in içerisinde kaç adet kelime olduğunu bulmanız isteniyor.

► HackerRank – CamelCase: https://www.hackerrank.com/challenges/camelcase/problem

► Problem açıklaması:

There is a sequence of words in CamelCase as a string of letters, s, having the following properties:

– It is a concatenation of one or more words consisting of English letters.

– All letters in the first word are lowercase.

– For each of the subsequent words, the first letter is uppercase and rest of the letters are lowercase.

Given s, determine the number of words in s.

Example

s = oneTwoThree

There are 3 words in the string: ‘one’, ‘Two’, ‘Three’.

Function Description

Complete the camelcase function in the editor below.

camelcase has the following parameter(s):

string s: the string to analyze

Returns

int: the number of words in s.

Sample Input

saveChangesInTheEditor

Sample Output

5

Explanation

String s contains five words:

1. save

2. Changes

3. In

4. The

5. Editor

HackerRank Çözümleri – Fibonacci Modified

HackerRank içerisinde bulunan “Fibonacci Modified” sorusunun açıklaması ve çözümü. Bu soruda fibonacci serisinin değiştirilmiş hali olarak, bir önceki sayının kendisi değil de karesinin toplamı şeklinde çözüm bulmanız isteniyor.

► HackerRank – Fibonacci Modified: https://www.hackerrank.com/challenges/fibonacci-modified/problem

► Problem açıklaması:

Implement a modified Fibonacci sequence using the following definition:

t(i + 2) = t(i) + t(i + 2)^2

Given three integers, t1, t2, and n, compute and print the nth term of a modified Fibonacci sequence.

Function Description

Complete the fibonacciModified function in the editor below. It must return the nth number in the sequence.

fibonacciModified has the following parameter(s):

int t1: an integer

int t2: an integer

int n: the iteration to report

Returns

int: the nth number in the sequence

Note: The value of t[n] may far exceed the range of a 64-bit integer. Many submission languages have libraries that can handle such large results but, for those that don’t (e.g., C++), you will need to compensate for the size of the result.

Sample Input

0 1 5

Sample Output

5

HackerRank Çözümleri – Extra Long Factorials

HackerRank içerisinde bulunan “Extra Long Factorials” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayının faktöriyelini bulmanız isteniyor. Bazı inputlar için faktöriyel 64-bit tam sayıyı aşabilir, bu yüzden daha büyük bir tam sayı tipi kullanmanız gerekiyor.

► HackerRank – Extra Long Factorials: https://www.hackerrank.com/challenges/extra-long-factorials/problem

► Problem açıklaması:

The factorial of the integer n, written n!, is defined as:

n! = n * (n – 1) * (n – 2) * … * 3 * 2 * 1

Function Description

Complete the extraLongFactorials function in the editor below. It should print the result and return.

extraLongFactorials has the following parameter(s):

n: an integer

Note: Factorials of n biggerThan 20 can’t be stored even in a 64-bit long long variable. Big integers must be used for such calculations. Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

We recommend solving this challenge using BigIntegers.

HackerRank Çözümleri – Fair Rations

HackerRank içerisinde bulunan “Fair Rations” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir tam sayı dizisi içerisinde, aşağıdaki kurala istinaden kaç adet sayı eklemesi yapılarak tüm sayıların çift sayı olabileceği soruluyor.

► HackerRank – Fair Rations: https://www.hackerrank.com/challenges/fair-rations/problem

► Problem açıklaması:

You are the benevolent ruler of Rankhacker Castle, and today you’re distributing bread. Your subjects are in a line, and some of them already have some loaves. Times are hard and your castle’s food stocks are dwindling, so you must distribute as few loaves as possible according to the following rules:

1. Every time you give a loaf of bread to some person i, you must also give a loaf of bread to the person immediately in front of or behind them in the line (i.e., persons i + 1 or i – 1).

2. After all the bread is distributed, each person must have an even number of loaves.

Given the number of loaves already held by each citizen, find and print the minimum number of loaves you must distribute to satisfy the two rules above. If this is not possible, print NO.

Sample Input 0

5 2 3 4 5 6

Sample Output 0

4

Explanation 0

The initial distribution is (2, 3, 4, 5, 6). The requirements can be met as follows:

1. Give loaf of bread each to the second and third people so that the distribution becomes (2, 4, 5, 5, 6).

2. Give loaf of bread each to the third and fourth people so that the distribution becomes (2, 4, 6, 6, 6).

Each of the N subjects has an even number of loaves after 4 loaves were distributed.

HackerRank Çözümleri – Minimum Loss

HackerRank içerisinde bulunan “Minimum Loss” sorusunun açıklaması ve çözümü. Bu soruda size ev fiyatları olarak verilen bir integer dizisi içerisinde, önce satın almak koşulu ile, satın alınıp sonrasında satış yapıldığında elde edebileceğiniz minimum zarar soruluyor.

► HackerRank – Minimum Loss: https://www.hackerrank.com/challenges/minimum-loss/problem

► Problem açıklaması:

Lauren has a chart of distinct projected prices for a house over the next several years. She must buy the house in one year and sell it in another, and she must do so at a loss. She wants to minimize her financial loss.

Example:

price = [20, 15, 8, 2, 12]

Her minimum loss is incurred by purchasing in year 2 at price[1] = 15 and reselling in year 5 at price[4] = 12. Return 15 – 12 = 3.

Function Description

Complete the minimumLoss function in the editor below.

minimumLoss has the following parameter(s):

int price[n]: home prices at each year

Returns int: the minimum loss possible

Sample Input 0

3

5 10 3

Sample Output 0

2

Sample Input

1

5 20 7 8 2 5

Sample Output 1

2

HackerRank Çözümleri – Find the Median

HackerRank içerisinde bulunan “Find the Median” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir seri içerisindeki medyan değerini, yani seriyi ortadan ikiye ayıran değeri bulmanız isteniyor.

► HackerRank – Find the Median: https://www.hackerrank.com/challenges/find-the-median/problem

► Problem açıklaması:

The median of a list of numbers is essentially its middle element after sorting. The same number of elements occur after it as before. Given a list of numbers with an odd number of elements, find the median?

https://en.wikipedia.org/wiki/Median

Example

arr = [5, 3, 1, 2, 4]

The sorted array arr = [1, 2, 3, 4, 5]. The middle element and the median is 3.

Sample Input 0

7

0 1 2 4 6 5 3

Sample Output 0

3

The sorted arr = [0, 1, 2, 3, 4, 5, 6]. It’s middle element is at arr[3] = 3.

HackerRank Çözümleri – Time Complexity: Primality

HackerRank içerisinde bulunan “Time Complexity: Primality” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir sayının asal sayı olup olmadığını O(sqrt(n)) zamanında bulmanız isteniyor.

► HackerRank – Time Complexity: Primality: https://www.hackerrank.com/challenges/ctci-big-o/problem

► Problem açıklaması:

A prime is a natural number greater than 1 that has no positive divisors other than 1 and itself. Given p integers, determine the primality of each integer and return Prime or Not prime on a new line.

Note: If possible, try to come up with an O(sqrt(n)) primality algorithm, or see what sort of optimizations you can come up with for an O(n) algorithm. Be sure to check out the Editorial after submitting your code.

Sample Input

STDIN Function

—– ——–

3 p = 3 (number of values to follow)

12 n = 12 (first number to check)

5 n = 5

7 n = 7

Sample Output

Not prime

Prime

Prime

Explanation

We check the following integers for primality:

n = 12 is divisible by numbers other than 1 and itself (i.e.: 2, 3, 4, 6).

n = 5 is only divisible 1 and itself.

n = 7 is only divisible 1 and itself.