Tag Archives: hackerrank çözümleri

Hackerrank “30 Days of Code” Çözümleri – Day 1: Data Types

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 1: Data Types” sorusunun açıklaması ve çözümü. Bu soruda sizin konsol‘dan aldığınız int, double ve string tipinde değişkenlerini “+” operatörü kullanarak konsola yazdırmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 1: Data Types: https://www.hackerrank.com/challenges/30-data-types/problem

► Problem açıklaması:

Objective

Today, we’re discussing data types. Check out the Tutorial tab for learning materials and an instructional video!

Task

Complete the code in the editor below. The variables , , and are already declared and initialized for you. You must:

1. Declare 3 variables: one of type int, one of type double, and one of type String.

2. Read 3 lines of input from stdin (according to the sequence given in the Input Format section below) and initialize your 3 variables.

3. Use the + operator to perform the following operations:

– Print the sum of i plus your int variable on a new line.

– Print the sum of d plus your double variable to a scale of one decimal place on a new line.

– Concatenate s with the string you read as input and print the result on a new line.

Note: If you are using a language that doesn’t support using + for string concatenation (e.g.: C), you can just print one variable immediately following the other on the same line. The string provided in your editor must be printed first, immediately followed by the string you read as input.

Input Format

The first line contains an integer that you must sum with i.

The second line contains a double that you must sum with d.

The third line contains a string that you must concatenate with s.

Output Format

Print the sum of both integers on the first line, the sum of both doubles (scaled to 1 decimal place) on the second line, and then the two concatenated strings on the third line.

Sample Input

12

4.0

is the best place to learn and practice coding!

Sample Output

16

8.0

HackerRank is the best place to learn and practice coding!

Explanation

When we sum the integers 4 and 12, we get the integer 16.

When we sum the floating-point numbers 4.0 and 4.0, we get 8.0.

When we concatenate HackerRank with is the best place to learn and practice coding!, we get HackerRank is the best place to learn and practice coding!.

You will not pass this challenge if you attempt to assign the Sample Case values to your variables instead of following the instructions above and reading input from stdin.

Hackerrank “30 Days of Code” Çözümleri – Day 0: Hello, World.

Hackerrank‘in 30 Days of Code içerisinde bulunan “Day 0: Hello, World” sorusunun açıklaması ve çözümü. Bu soruda sizin console’dan alınan bir string’i yine console’a yeni bir satırda yazdırmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 0: Hello, World: https://www.hackerrank.com/challenges/30-hello-world/problem

► Problem açıklaması:

Objective

In this challenge, we review some basic concepts that will get you started with this series. You will need to use the same (or similar) syntax to read input and write output in challenges throughout HackerRank. Check out the Tutorial tab for learning materials and an instructional video!

Task

To complete this challenge, you must save a line of input from stdin to a variable, print Hello, World. on a single line, and finally print the value of your variable on a second line.

You’ve got this!

Note: The instructions are Java-based, but we support submissions in many popular languages. You can switch languages using the drop-down menu above your editor, and the inputString variable may be written differently depending on the best-practice conventions of your submission language.

Input Format

A single line of text denoting inputString (the variable whose contents must be printed).

Output Format

Print Hello, World. on the first line, and the contents of inputString on the second line.

Sample Input

Welcome to 30 Days of Code!

Sample Output

Hello, World.

Welcome to 30 Days of Code!

Explanation

On the first line, we print the string literal Hello, World.. On the second line, we print the contents of the inputString variable which, for this sample case, happens to be Welcome to 30 Days of Code!. If you do not print the variable’s contents to stdout, you will not pass the hidden test case.

HackerRank Çözümleri – Chocolate Feast

HackerRank içerisinde bulunan “Chocolate Feast” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir çikolata dükkanı için, belirli bir sayıda çikolata kabı getirdiğinizde belirli bir sayıda çikolata barı alabileceğiniz, belirli bir sayıda çikolata ile maksimum promosyonlar ile birlikte kaç adet çikolata yiyebileceğinizi hesaplamanız isteniyor.

► HackerRank – Chocolate Feast: https://www.hackerrank.com/challenges/chocolate-feast/problem

► Problem açıklaması:

Little Bobby loves chocolate. He frequently goes to his favorite 5&10 store, Penny Auntie, to buy them. They are having a promotion at Penny Auntie. If Bobby saves enough wrappers, he can turn them in for a free chocolate.

Example

n = 15

c = 3

m = 2

He has 15 to spend, bars cost 3, and he can turn in 2 wrappers to receive another bar. Initially, he buys 5 bars and has 5 wrappers after eating them. He turns in 4 of them, leaving him with 1, for 2 more bars. After eating those two, he has 3 wrappers, turns in 2 leaving him with 1 wrapper and his new bar. Once he eats that one, he has 2 wrappers and turns them in for another bar. After eating that one, he only has 1 wrapper, and his feast ends. Overall, he has eaten 5 + 2 + 1 + 1 = 9 bars.

Function Description

Complete the chocolateFeast function in the editor below.

chocolateFeast has the following parameter(s):

int n: Bobby’s initial amount of money

int c: the cost of a chocolate bar

int m: the number of wrappers he can turn in for a free bar

Returns

int: the number of chocolates Bobby can eat after taking full advantage of the promotion

Note: Little Bobby will always turn in his wrappers if he has enough to get a free chocolate.

Sample Input

STDIN Function

—– ——–

3 t = 3 (test cases)

10 2 5 n = 10, c = 2, m = 5 (first test case)

12 4 4 n = 12, c = 4, m = 4 (second test case)

6 2 2 n = 6, c = 2, m = 2 (third test case)

Sample Output

6

3

5

HackerRank Çözümleri – Repeated String

HackerRank içerisinde bulunan “Repeated String” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir string ve n sayısı için, verilen string’i n uzunlukta olacak şekilde karakterlerini yazarak sonuç string’inde kaç tane ‘a’ karakteri olduğunu bulmanız isteniyor.

► HackerRank – Repeated String: https://www.hackerrank.com/challenges/repeated-string/problem

► Problem açıklaması:

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.

Example

s = ‘abcac’

n = 10

The substring we consider is abcacabcac, the first 10 characters of the infinite string. There are 4 occurrences of a in the substring.

Function Description

Complete the repeatedString function in the editor below.

repeatedString has the following parameter(s):

s: a string to repeat

n: the number of characters to consider

Returns

int: the frequency of a in the substring

Input Format

The first line contains a single string, s.

The second line contains an integer, n.

Sample Input

Sample Input 0

aba

10

Sample Output 0

7

Explanation 0

The first n = 10 letters of the infinite string are abaabaabaa. Because there are 7 a’s, we return 7.

Sample Input 1

a

1000000000000

Sample Output 1

1000000000000

Explanation 1

Because all of the first n = 1000000000000 letters of the infinite string are a, we return 1000000000000 .

HackerRank Çözümleri – Cycle Detection

HackerRank içerisinde bulunan “Cycle Detection” sorusunun açıklaması ve çözümü. Bu soruda size verilen bir linked list içerisinde döngüsel bir yapı olup olmadığını bulmanız isteniyor.

► HackerRank – Cycle Detection: https://www.hackerrank.com/challenges/detect-whether-a-linked-list-contains-a-cycle/problem

► Problem açıklaması:

A linked list is said to contain a cycle if any node is visited more than once while traversing the list. Given a pointer to the head of a linked list, determine if it contains a cycle. If it does, return 1. Otherwise, return 0.

Example

head refers to the list of nodes 1 — 2 — 3 — NULL

The numbers shown are the node numbers, not their data values. There is no cycle in this list so return 0.

head refers to the list of nodes 1 — 2 — 3 — 1 — NULL

There is a cycle where node 3 points back to node 1, so return 1.

Function Description

Complete the has_cycle function in the editor below.

It has the following parameter:

SinglyLinkedListNode pointer head: a reference to the head of the list

Returns

int:1 if there is a cycle or 0 if there is not

Note: If the list is empty, head will be null.

Input Format

The code stub reads from stdin and passes the appropriate argument to your function. The custom test cases format will not be described for this question due to its complexity. Expand the section for the main function and review the code if you would like to figure out how to create a custom case.

HackerRank Çözümleri – Sherlock and Squares

HackerRank içerisinde bulunan “Sherlock and Squares” sorusunun açıklaması ve çözümü. Bu soruda verilen iki sayı arasındaki tam kare olan sayıların adetini bulmanız istenir.

► HackerRank – Sherlock and Squares: https://www.hackerrank.com/challenges/sherlock-and-squares/problem

► Problem açıklaması:

Watson likes to challenge Sherlock’s math ability. He will provide a starting and ending value that describe a range of integers, inclusive of the endpoints. Sherlock must determine the number of square integers within that range.

Note: A square integer is an integer which is the square of an integer, e.g. 1, 4, 9, 16, 25

Example

a = 24

b = 49

There are three square integers in the range: 25, 36 and 49. Return 3.

Function Description

Complete the squares function in the editor below. It should return an integer representing the number of square integers in the inclusive range from a to b.

squares has the following parameter(s):

int a: the lower range boundary

int b: the upper range boundary

Returns

int: the number of square integers in the range.

Input Format

The first line contains q, the number of test cases. Each of the next q lines contains two space-separated integers, a and b, the starting and ending integers in the ranges.

Sample Input

2

3 9

17 24

Sample Output

2

0

HackerRank Çözümleri – Viral Advertising

HackerRank içerisinde bulunan “Viral Advertising” sorusunun açıklaması ve çözümü. HackerLand Enterprise firması yeni bir reklam stratejisi üzerinde çalışıyor. İlk günde bu reklamı 5 kişiye gösteriyorlar sosyal medyada. Ve bu kişi sayısının yarısı 3’er arkadaşı ile paylaşıyor bu reklamı. Size verilen n gün sonra bu reklamı kaç kişi gördüğünü bulmanız isteniyor.

► HackerRank – Viral Advertising: https://www.hackerrank.com/challenges/strange-advertising/problem

► Problem açıklaması:

HackerLand Enterprise is adopting a new viral advertising strategy. When they launch a new product, they advertise it to exactly 5 people on social media.

On the first day, half of those 5 people (i.e., 5 / 2 = 2) like the advertisement and each shares it with 3 of their friends. At the beginning of the second day, (5 / 2 * 3 = 2 *3 = 6) people receive the advertisement.

Each day, floor(recipients / 2) of the recipients like the advertisement and will share it with 3 friends on the following day. Assuming nobody receives the advertisement twice, determine how many people have liked the ad by the end of a given day, beginning with launch day as day 1.

Example

n = 5.

Day Shared Liked Cumulative

1 5 2 2

2 6 3 5

3 9 4 9

4 12 6 15

5 18 9 24

The progression is shown above. The cumulative number of likes on the 5th day is 24.

Function Description

Complete the viralAdvertising function in the editor below.

viralAdvertising has the following parameter(s):

int n: the day number to report

Returns

int: the cumulative likes at that day

Input Format

A single integer, , the day number.

Constraints

Sample Input

3

Sample Output

9

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