leticiacamposs2 / hackerrank-java-web-challenges

💻☕ Repository with HackerRank (Problem Solving) backend challenges solved with Java language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HackerRank Java Web Solutions

Challenges of Backend with Java by WoMakersCode 🦋

✏️ Todolist

✔️ Java Stdin and Stdout I

Task

In this challenge, you must read 3 integers from stdin and then print them to stdout. Each integer must be printed on a new line.

Solution: java-stdin-and-stdout-1

✔️ Java If-Else

Task

Given an integer, n, perform the following conditional actions:

  • If n is odd, print Weird
  • If n is even and in the inclusive range of 2 to 5, print Not Weird
  • If n is even and in the inclusive range of 6 to 20, print Weird
  • If n is even and greater than 20, print Not Weird Complete the stub code provided in your editor to print whether or not is weird.

Explanation

Sample Case 0: n = 3 n is odd and odd numbers are weird, so we print Weird.

Sample Case 1: n = 24 n > 20 and n is even, so it isn't weird. Thus, we print Not Weird.

Solution: java-if-else

✔️ Java Date and Time

Task

You are given a date. You just need to write the method, getDay, which returns the day on that date. To simplify your task, we have provided a portion of the code in the editor.

Example

month = 8
day = 14
yaer = 2017

The method should return MONDAY as the day on that date.

calendar

Function Description

Complete the findDay function in the editor below.

findDay has the following parameters:

  • int: month
  • int: day
  • int: year

Returns

  • string: the day of the week in capital letters

Sample Input

08 05 2015

Sample Output

WEDNESDAY

Solution: java-date-and-time

✔️ Java Loops I

Task

Given an integer, N, print its first 10 multiples. Each multiple N x i (where 1 <= i <= 10) should be printed on a new line in the form: N x i = result.

Solution: java-loops-i

✔️ Java Int to String

Task

If your code successfully converts n into a string s the code will print "Good job". Otherwise it will print "Wrong answer".

Solution: java-int-to-string

✔️ Java String Reverse

Task

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward.

Given a string A, print Yes if it is a palindrome, print No otherwise.

Solution: java-string-reverse

✔️ Java BigInteger

In this problem, you have to add and multiply huge numbers! These numbers are so big that you can't contain them in any ordinary data types like a long integer.

Use the power of Java's BigInteger class and solve this problem.

Sample Input

1234
20

Sample Output

1254
24680

Explanation

1234 + 20 = 1254
1234 * 20 = 24680

Solution: java-biginteger

✔️ Java 1D Array

Task

  1. Reads an integer from stdin and saves it to a variable, n, denoting some number of integers.
  2. Reads n integers corresponding to a0, a1, ... an-1 from stdin and saves each integer to a variable, val.
  3. Attempts to print each element of an array of integers named a.

Solution: java-1d-array-introduction

✔️ Java Abstract Class

You have to create another class that extends the abstract class. Then you can create an instance of the new class.

Notice that setTitle method is abstract too and has no body. That means you must implement the body of that method in the child class.

Sample Input

A tale of two cities

Sample Output

The title is: A tale of two cities

Solution: java-abstract-class

✔️ Java Datatypes

Task

Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this exercise, we'll work with the primitives used to hold integer values (byte, short, int, and long):

  • A byte is an 8-bit signed integer.
  • A short is a 16-bit signed integer.
  • An int is a 32-bit signed integer.
  • A long is a 64-bit signed integer.

Given an input integer, you must determine which primitive data types are capable of properly storing that input.

Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Solution: java-datatypes

✔️ Java Arraylist

Input Format

The first line has an integer n. In each of the next n lines there will be an integer d denoting number of integers on that line and then there will be d space-separated integers. In the next line there will be an integer q denoting number of queries. Each query will consist of two integers x and y.

Each number will fit in signed integer. Total number of integers in n lines will not cross 10 5.

Output Format

In each line, output the number located in Yth position of Xth line. If there is no such position, just print "ERROR!"

Sample Input

5
5 41 77 74 22 44
1 12
4 37 34 36 52
0
3 20 22 33
5
1 3
3 4
3 1
4 3
5 5

Sample Output

74
52
37
ERROR!
ERROR!

Explanation

The diagram below explains the queries:

java-arraylist

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Solution: java-arraylist

✔️ Java Exception Handling (Try-catch)

Java has built-in mechanism to handle exceptions. Using the try statement we can test a block of code for errors. The catch block contains the code that says what to do if exception occurs.

This problem will test your knowledge on try-catch block.

You will be given two integers x and y as input, you have to compute x/y. If x and y are not 32 bit signed integers or if y is zero, exception will occur and you have to report it. Read sample Input/Output to know what to report in case of exceptions.

Solution: java-exception-handling-try-catch

✔️ Java Exception Handling

You are required to compute the power of a number by implementing a calculator. Create a class MyCalculator which consists of a single method long power(int, int). This method takes two integers, n and p, as parameters and finds Np. If either n or p is negative, then the method must throw an exception which says "n or p should not be negative". Also, if both n and p are zero, then the method must throw an exception which says "n and p should not be zero".

For example, -4 and -5 would result in java.lang.Exception: n or p should not be negative.

Solution: java-exception-handling

✔️ Java String Tokens

Given a string, s, matching the regular expression [A-Za-z !,?._'@]+, split the string into tokens. We define a token to be one or more consecutive English alphabetic letters. Then, print the number of tokens, followed by each token on a new line.

Output Format

On the first line, print an integer, n, denoting the number of tokens in string s (they do not need to be unique). Next, print each of the tokens on a new line in the same order as they appear in input string s.

Solution: java-string-tokens

✔️ Java Dequeue

In computer science, a double-ended queue (dequeue, often abbreviated to deque, pronounced deck) is an abstract data type that generalizes a queue, for which elements can be added to or removed from either the front (head) or back (tail).

Deque interfaces can be implemented using various types of collections such as LinkedList or ArrayDeque classes. For example, deque can be declared as:

Deque deque = new LinkedList<>();
or
Deque deque = new ArrayDeque<>();

You can find more details about Deque here.

In this problem, you are given N integers. You need to find the maximum number of unique integers among all the possible contiguous subarrays of size M.

Note: Time limit is 3 second for this problem.

Solution: java-queue

✔️ Java Priority Queue

In this problem we will test your knowledge on Java Priority Queue.

There are a number of students in a school who wait to be served. Two types of events, ENTER and SERVED, can take place which are described below.

  • ENTER: A student with some priority enters the queue to be served.
  • SERVED: The student with the highest priority is served (removed) from the queue. A unique id is assigned to each student entering the queue. The queue serves the students based on the following criteria (priority criteria):
  1. The student having the highest Cumulative Grade Point Average (CGPA) is served first.
  2. Any students having the same CGPA will be served by name in ascending case-sensitive alphabetical order.
  3. Any students having the same CGPA and name will be served in ascending order of the id.

Create the following two classes:

  • The Student class should implement:

  • The constructor Student(int id, String name, double cgpa).

  • The method int getID() to return the id of the student.

  • The method String getName() to return the name of the student.

  • The method double getCGPA() to return the CGPA of the student.

  • The Priorities class should implement the method List< Student > getStudents(List< String > events) to process all the given events and return all the students yet to be served in the priority order.

Solution: java-priority-queue

✔️ Can You Access?

You are given a class Solution and an inner class Inner.Private. The main method of class Solution takes an integer num as input. The powerof2 in class Inner.Private checks whether a number is a power of 2. You have to call the method powerof2 of the class Inner.Private from the main method of the class Solution.

Sample Input

 8

Sample Output

8 is power of 2
An instance of class: Solution.Inner.Private has been created

Solution: can-you-access

✔️ Java Comparator

We define the following terms:

  • Lexicographical Order, also known as alphabetic or dictionary order, orders characters as follows:

    A < B < ... < Y < Z < a < b < ... < y < z

For example, ball < cat, dog < dorm, Happy < happy, Zoo < ball.

  • A substring of a string is a contiguous block of characters in the string. For example, the substrings of abc are a, b, c, ab, bc, and abc. Given a string, , and an integer, , complete the function so that it finds the lexicographically smallest and largest substrings of length .

Solution: java-comparator

✔️ Java BitSet

Java's BitSet class implements a vector of bit values (i.e.: false(0) or true(1)) that grows as needed, allowing us to easily manipulate bits while optimizing space (when compared to other collections). Any element having a bit value of 1 is called a set bit.

Given 2 BitSets, and , of size N where all bits in both BitSets are initialized to 0, perform a series of M operations. After each operation, print the number of set bits in the respective BitSets as two space-separated integers on a new line.

Input Format

The first line contains 2 space-separated integers, N (the length of both BitSets and ) and M (the number of operations to perform), respectively. The M subsequent lines each contain an operation in one of the following forms:

  • AND< set > < set >
  • OR< set > < set >
  • XOR< set > < set >
  • FLIP< set > < index >
  • SET< set > < index >

In the list above, < set > is the integer 1 or 2, where 1 denotes and 2 denotes . < index > is an integer denoting a bit's index in the BitSet corresponding to < set > .

For the binary operations AND, OR, and XOR, operands are read from left to right and the BitSet resulting from the operation replaces the contents of the first operand. For example:

AND 2 1

is the left operand, and is the right operand. This operation should assign the result of B² ^ B¹ to B².

Solution: java-bitset


Challenges to solve

About

💻☕ Repository with HackerRank (Problem Solving) backend challenges solved with Java language


Languages

Language:Java 100.0%