Category Archives: Hackerrank

Hackerrank “30 Days of Code” Çözümleri – Day 18: Queues and Stacks

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 18: Queues and Stacks” sorusunun açıklaması ve çözümü. Bu soruda queue ve stack yapısına göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 18: Queues and Stacks: https://www.hackerrank.com/challenges/30-queues-stacks/problem

► Problem açıklaması:

Welcome to Day 18! Today we’re learning about Stacks and Queues. Check out the Tutorial tab for learning materials and an instructional video!

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backwards and forwards. Can you determine if a given string, s, is a palindrome?

To solve this challenge, we must first take each character in s, enqueue it in a queue, and also push that same character onto a stack. Once that’s done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means s isn’t a palindrome).

Write the following declarations and implementations:

Two instance variables: one for your stack, and one for your queue.

A void pushCharacter(char ch) method that pushes a character onto a stack.

A void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.

A char popCharacter() method that pops and returns the character at the top of the stack instance variable.

A char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.

Input Format

You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string s. It then calls the methods specified above to pass each character to your instance variables.

Constraints

is composed of lowercase English letters.

Output Format

You are not responsible for printing any output to stdout.

Sample Input

racecar

Sample Output

The word, racecar, is a palindrome.

Hackerrank “30 Days of Code” Çözümleri – Day 17: More Exceptions

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 17: More Exceptions” sorusunun açıklaması ve çözümü. Bu soruda exception kavramına göz atmaya devam ettik.

► Hackerrank 30 Days of Code Çözümleri – Day 17: More Exceptions: https://www.hackerrank.com/challenges/30-more-exceptions/problem

► Problem açıklaması:

Objective

Yesterday’s challenge taught you to manage exceptional situations by using try and catch blocks. In today’s challenge, you will practice throwing and propagating an exception. Check out the Tutorial tab for learning materials and an instructional video.

Task

Write a Calculator class with a single method: int power(int,int). The power method takes two integers, n and p, as parameters and returns the integer result of n^p. If either n or p is negative, then the method must throw an exception with the message: n and p should be non-negative.

Note: Do not use an access modifier (e.g.: public) in the declaration for your Calculator class.

Input Format

Input from stdin is handled for you by the locked stub code in your editor. The first line contains an integer, T, the number of test cases. Each of the T subsequent lines describes a test case in 2 space-separated integers that denote n and p, respectively.

Constraints

No Test Case will result in overflow for correctly written code.

Output Format

Output to stdout is handled for you by the locked stub code in your editor. There are T lines of output, where each line contains the result of n^p as calculated by your Calculator class’ power method.

Sample Input

4

3 5

2 4

-1 -2

-1 3

Sample Output

243

16

n and p should be non-negative

n and p should be non-negative

Hackerrank “30 Days of Code” Çözümleri – Day 16: Exceptions – String to Integer

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 16: Exceptions – String to Integer” sorusunun açıklaması ve çözümü. Bu soruda try catch ile exception handling kısmında kısaca baktık.

► Hackerrank 30 Days of Code Çözümleri – Day 16: Exceptions – String to Integer: https://www.hackerrank.com/challenges/30-exceptions-string-to-integer/problem

► Problem açıklaması:

Objective

Today, we’re getting started with Exceptions by learning how to parse an integer from a string and print a custom error message. Check out the Tutorial tab for learning materials and an instructional video!

Task

Read a string, S, and print its integer value; if S cannot be converted to an integer, print Bad String.

Note: You must use the String-to-Integer and exception handling constructs built into your submission language. If you attempt to use loops/conditional statements, you will get a 0 score.

Input Format

A single string, S.

Output Format

Print the parsed integer value of S, or Bad String if S cannot be converted to an integer.

Sample Input 0

3

Sample Output 0

3

Sample Input 1

za

Sample Output 1

Bad String

Explanation

Sample Case 0 contains an integer, so it should not raise an exception when we attempt to convert it to an integer. Thus, we print the 3.

Sample Case 1 does not contain any integers, so an attempt to convert it to an integer will raise an exception. Thus, our exception handler prints Bad String.

Hackerrank “30 Days of Code” Çözümleri – Day 15: Linked List

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 15: Linked List” sorusunun açıklaması ve çözümü. Bu soruda linked list veri yapısına ve içerisindeki node’larda iterasyon yapılmasına göz atıyoruz.

► Hackerrank 30 Days of Code Çözümleri – Day 15: Linked List: https://www.hackerrank.com/challenges/30-linked-list/problem

► Problem açıklaması:

Objective

Today we will work with a Linked List. Check out the Tutorial tab for learning materials and an instructional video.

A Node class is provided for you in the editor. A Node object has an integer data field, data, and a Node instance pointer, next, pointing to another node (i.e.: the next node in the list).

A Node insert function is also declared in your editor. It has two parameters: a pointer, head, pointing to the first node of a linked list, and an integer, data, that must be added to the end of the list as a new Node object.

Task

Complete the insert function in your editor so that it creates a new Node (pass data as the Node constructor argument) and inserts it at the tail of the linked list referenced by the head parameter. Once the new node is added, return the reference to the head node.

Note: The head argument is null for an empty list.

Input Format

The first line contains T, the number of elements to insert.

Each of the next T lines contains an integer to insert at the end of the list.

Output Format

Return a reference to the head node of the linked list.

Sample Input

STDIN Function

—– ——–

4 T = 4

2 first data = 2

3

4

1 fourth data = 1

Sample Output

2 3 4 1

Explanation

T = 4, so your method will insert 4 nodes into an initially empty list. First the code returns a new node that contains the data value 2 as the head of the list. Then create and insert nodes 3, 4, and a at the tail of the list.

Hackerrank “30 Days of Code” Çözümleri – Day 14: Scope

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 14: Scope” sorusunun açıklaması ve çözümü. Bu soruda scope kavramına göz atmaya çalıştık.

► Hackerrank 30 Days of Code Çözümleri – Day 14: Scope: https://www.hackerrank.com/challenges/30-scope/problem

► Problem açıklaması:

Objective

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

The absolute difference between two integers, a and b, is written as |a – b|. The maximum absolute difference between two integers in a set of positive integers, elements, is the largest absolute difference between any two integers in _elements.

The Difference class is started for you in the editor. It has a private integer array (elements) for storing N non-negative integers, and a public integer (maximumDifference) for storing the maximum absolute difference.

Task

Complete the Difference class by writing the following:

A class constructor that takes an array of integers as a parameter and saves it to the instance variable. A computeDifference method that finds the maximum absolute difference between any numbers in and stores it in the instance variable.

Input Format

You are not responsible for reading any input from stdin. The locked Solution class in the editor reads in lines of input. The first line contains N, the size of the elements array. The second line has space-separated integers that describe the array.

Output Format

You are not responsible for printing any output; the Solution class will print the value of the maximumDifference instance variable.

Sample Input

STDIN Function

—– ——–

3 __elements[] size N = 3

1 2 5 __elements = [1, 2, 5]

Sample Output

4

Hackerrank “30 Days of Code” Çözümleri – Day 13: Abstract Classes

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 13: Abstract Classes” sorusunun açıklaması ve çözümü. Bu soruda abstract sınıflara ve base anahtar sözcüğüne göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 13: Abstract Classes: https://www.hackerrank.com/challenges/30-abstract-classes/problem

► Problem açıklaması:

Objective

Today, we will extend what we learned yesterday about Inheritance to Abstract Classes. Because this is a very specific object oriented concept, submissions are limited to the few languages that use this construct. Check out the Tutorial tab for learning materials and an instructional video.

Task

Given a Book class and a Solution class, write a MyBook class that does the following:

Inherits from Book

Has a parameterized constructor taking these 3 parameters:

string title

string author

int price

Implements the Book class’ abstract display() method so it prints these 3 lines:

Title , a space, and then the current instance’s title.

Author , a space, and then the current instance’s author.

Price , a space, and then the current instance’s price.

Note: Because these classes are being written in the same file, you must not use an access modifier (e.g.: public) when declaring MyBook or your code will not execute.

Input Format

You are not responsible for reading any input from stdin. The Solution class creates a Book object and calls the MyBook class constructor (passing it the necessary arguments). It then calls the display method on the Book object.

Output Format

Title: $title

Author: $author

Price: $price

Note: The $ is prepended to variable names to indicate they are placeholders for variables.

Sample Input

The following input from stdin is handled by the locked stub code in your editor:

The Alchemist

Paulo Coelho

248

Sample Output

The following output is printed by your display() method:

Title: The Alchemist

Author: Paulo Coelho

Price: 248

Hackerrank “30 Days of Code” Çözümleri – Day 11: 2D Arrays

Hackerrank‘in 30 Days of Code içerisinde bulunan “Day 11: 2D Arrays” sorusunun açıklaması ve çözümü. Bu soruda verilen bir list of list şeklindeki yapıda, kum saati şeklindeki elemanlarının toplamının maksimum değerini bulmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 11: 2D Arrays: https://www.hackerrank.com/challenges/30-2d-arrays/problem

► Problem açıklaması:

Objective

Today, we are building on our knowledge of arrays by adding another dimension. Check out the Tutorial tab for learning materials and an instructional video.

Context

Given a 6×6 2D Array, A:

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

We define an hourglass in to be a subset of values with indices falling in this pattern in A’s graphical representation:

a b c

d

e f g

There are 16 hourglasses in A, and an hourglass sum is the sum of an hourglass’ values.

Task

Calculate the hourglass sum for every hourglass in A, then print the maximum hourglass sum.

Hackerrank “30 Days of Code” Çözümleri – Day 10: Binary Numbers

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 10: Binary Numbers” sorusunun açıklaması ve çözümü. Bu soruda verilen sayının binary gösteriminin içerdiği yan yana 1’lerin maksimum sayısını bulmanız isteniyor.

► Hackerrank 30 Days of Code Çözümleri – Day 10: Binary Numbers: https://www.hackerrank.com/challenges/30-binary-numbers/problem

► Problem açıklaması:

Objective

Today, we’re working with binary numbers. Check out the Tutorial tab for learning materials and an instructional video!

Task

Given a base-10 integer, n, convert it to binary (base-2). Then find and print the base-10 integer denoting the maximum number of consecutive ‘1s in n’s binary representation. When working with different bases, it is common to show the base as a subscript.

Example

n = 125

The binary representation of 125 is 1111101. In base 10, there are 5 and 1 consecutive ones in two groups. Print the maximum, 5.

Input Format

A single integer, n.

Output Format

Print a single base-10 integer that denotes the maximum number of consecutive 1’s in the binary representation of n.

Sample Input 1

5

Sample Output 1

1

Sample Input 2

13

Sample Output 2

2

Hackerrank “30 Days of Code” Çözümleri – Day 9: Recursion 3

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 9: Recursion 3” sorusunun açıklaması ve çözümü. Bu soruda rekürsif fonksiyonlara giriş yapıyoruz. Derler ki; “Rekürsif fonksiyonları anlamak için önce rekürsif fonksiyonları anlamanız gerekir“.

► Hackerrank 30 Days of Code Çözümleri – Day 9: Recursion 3: https://www.hackerrank.com/challenges/30-recursion/problem

► Problem açıklaması:

Objective

Today, we are learning about an algorithmic concept called recursion. Check out the Tutorial tab for learning materials and an instructional video.

Recursive Method for Calculating Factorial Function Description Complete the factorial function in the editor below. Be sure to use recursion.

factorial has the following paramter:

int n: an integer

Returns

int: the factorial of n

Note: If you fail to use recursion or fail to name your recursive function factorial or Factorial, you will get a score of 0.

Input Format

A single integer, n (the argument to pass to factorial).

Constraints

– 2 lessEqual n lessEqual 12

– Your submission must contain a recursive function named factorial.

Sample Input

3

Sample Output

6

Explanation

Consider the following steps. After the recursive calls from step 1 to 3, results are accumulated from step 3 to 1.

factorial(3) = 3 x factorial(2) = 3 x 2 = 6

factorial(2) = 2 x factorial(1) = 2 x 1 = 2

factorial(1) = 1

Hackerrank “30 Days of Code” Çözümleri – Day 8: Dictionaries and Maps

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 8: Dictionaries and Maps” sorusunun açıklaması ve çözümü. Bu soruda dictionary ve hash table mantığına göz atıyoruz.

► Hackerrank 30 Days of Code Çözümleri – Day 8: Dictionaries and Maps: https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem

► Problem açıklaması:

Objective

Today, we’re learning about Key-Value pair mappings using a Map or Dictionary data structure. Check out the Tutorial tab for learning materials and an instructional video!

Task

Given n names and phone numbers, assemble a phone book that maps friends’ names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.

Note: Your phone book should be a Dictionary/Map/HashMap data structure.

Input Format

The first line contains an integer, n, denoting the number of entries in the phone book. Each of the n subsequent lines describes an entry in the form of 2 space-separated values on a single line. The first value is a friend’s name, and the second value is an 8-digit phone number.

After the n lines of phone book entries, there are an unknown number of lines of queries. Each line (query) contains a name to look up, and you must continue reading lines until there is no more input.

Note: Names consist of lowercase English alphabetic letters and are first names only.

Output Format

On a new line for each query, print Not found if the name has no corresponding entry in the phone book; otherwise, print the full name and phoneNumber in the format name=phoneNumber.

Sample Input

3

sam 99912222

tom 11122222

harry 12299933

sam

edward

harry

Sample Output

sam=99912222

Not found

harry=12299933