Tag Archives: hackerrank çözümleri

Hackerrank “30 Days of Code” Çözümleri – Day 19: Interfaces

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 19: Interfaces” sorusunun açıklaması ve çözümü. Bu soruda interface kavramına kısaca göz attık.

► Hackerrank 30 Days of Code Çözümleri – Day 19: Interfaces: https://www.hackerrank.com/challenges/30-interfaces/problem

► Problem açıklaması:

Objective

Today, we’re learning about Interfaces. Check out the Tutorial tab for learning materials and an instructional video!

Task

The AdvancedArithmetic interface and the method declaration for the abstract divisorSum(n) method are provided for you in the editor below.

Complete the implementation of Calculator class, which implements the AdvancedArithmetic interface. The implementation for the divisorSum(n) method must return the sum of all divisors of n.

Example

n = 25

The divisors of 25 are 1, 5, 25. Their sum is 31.

n = 20

The divisors of 20 are 1, 2, 4, 5, 10, 20 and their sum is 42.

Input Format

A single line with an integer, n.

Output Format

You are not responsible for printing anything to stdout. The locked template code in the editor below will call your code and print the necessary output.

Sample Input

6

Sample Output

I implemented: AdvancedArithmetic

12

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 12: Inheritance

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 12: Inheritance” sorusunun açıklaması ve çözümü. Bu soruda inheritance kavramına göz atıyoruz.

► Hackerrank 30 Days of Code Çözümleri – Day 12: Inheritance: https://www.hackerrank.com/challenges/30-inheritance/problem

► Problem açıklaması:

Objective

Today, we’re delving into Inheritance. Check out the attached tutorial for learning materials and an instructional video.

Task

You are given two classes, Person and Student, where Person is the base class and Student is the derived class. Completed code for Person and a declaration for Student are provided for you in the editor. Observe that Student inherits all the properties of Person.

Complete the Student class by writing the following:

A Student class constructor, which has parameters:

A string, firstName.

A string, lastName.

An integer, idNumber.

An integer array (or vector) of test scores, scores.

A char calculate() method that calculates a Student object’s average and returns the grade character representative of their calculated average:

Input Format

The locked stub code in the editor reads the input and calls the Student class constructor with the necessary arguments. It also calls the calculate method which takes no arguments.

The first line contains firstName, lastName, and idNumber, separated by a space. The second line contains the number of test scores. The third line of space-separated integers describes scores.

Output Format

Output is handled by the locked stub code. Your output will be correct if your Student class constructor and calculate() method are properly implemented.

Sample Input

Heraldo Memelli 8135627

2

100 80

Sample Output

Name: Memelli, Heraldo

ID: 8135627

Grade: O

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