0% found this document useful (0 votes)
4 views

Additional Coding Problem

The document outlines a series of coding exercises that include creating functions for checking palindromes, finding the longest increasing sequence in an array, calculating a single-digit sum from a large number, grouping anagrams, and implementing a Battleship game using an 8x8 map. It also includes tasks related to manipulating ArrayLists and HashMaps, handling exceptions, and demonstrating iterator usage with Sets. Each exercise provides input and output formats along with example scenarios for clarity.

Uploaded by

Vishal Dutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Additional Coding Problem

The document outlines a series of coding exercises that include creating functions for checking palindromes, finding the longest increasing sequence in an array, calculating a single-digit sum from a large number, grouping anagrams, and implementing a Battleship game using an 8x8 map. It also includes tasks related to manipulating ArrayLists and HashMaps, handling exceptions, and demonstrating iterator usage with Sets. Each exercise provides input and output formats along with example scenarios for clarity.

Uploaded by

Vishal Dutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Coding Exercises

1)

2)

3) You have to create a function isPalindrome() and it should return lambda expression
performing the action:
isPlaindrome(): The lambda expression should return true if number is palindrome
or false if it is not palindrome.
Input Format:

The first line of input consists of number of test cases, T.


Next T lines consist of integers.

Output Format

For each test case, Print PALINDROME or NOT PALINDROME by performing


the above operation.
Input:

2

…..
121
13

Output:

PALINDROME
NOT PALINDROME

4) Given an integer array, find the length of its longest increasing sequence.
The prototype of the function is as below:
public static void longestIncreasingSeq(int input1, int[] input2);

The function takes as input an integer input1 and an integer array input2, containing
input1 number of integers, and sets the output1 variable to the length of its LIS
(longest increasing sequence).

Example 1: input1 = 9 input2[] = {11,3,4,7,8,12,2,3,7} output1 should be 5.


Explanation: In the given array input2, the two increasing sequences are “3,4,7,8,12”
and “2,3,7”. The first sequence i.e. “3,4,7,8,12” is the longer one containing five items.
So, the longest increasing sequence = 5.

Example 2: input1 = 4 input2[] = {1,3,2,1} output1 should be 2 Explanation: In the


given array input2, the increasing sequence is “1,3” containing two items. So, the
longest increasing sequence = 2.

Example 3: input1 = 12 input2[] = {12,17,21,3,7,9,10,11,33,100,4,8} output1 should


be 7

5) DigitSum

The labels on a trader's boxes display a large number (integer). The trader wants to label
the boxes with a single digit ranging from 1 to 9. He decides to perform digit sum on this
large number, continuously till he gets a single digit number. NOTE: In mathematics, the
digit-sum of a given integer is the sum of all its digits, (e.g.: the digit sum of 84001 is
calculated as 8+4+0+0+1 = 13, the digit sum of 13 is 1+3 = 4). Write a function (method)
that takes as input a large number and returns a single digit by performing continuous
digitSum on this number, and on the resulting numbers, till the resulting number is a
single digit number in the range 1 to 9.
Example 1: If the large number whose single-digit digitSum is to be found is 976592,
the process is as below – 9+7+6+5+9+2 = 38 3+8 = 11 1+1 = 2 Thus, the single-digit
digitSum
for the number 976592 is 2.
Example 2: If the large number whose single-digit digitSum is to be found is 123456, the
process is as below – 1+2+3+4+5+6 = 21 2+1 = 3 Thus, the single-digit digitSum for the
number 123456 is 3. For negative numbers, the result should also be in negative.
Example 3: If the large number whose single-digit digitSum is to be found is -123456,
the answer would be -3. The prototype of the function is: int digitSum(int input1);
where, input1 is the large number whose single-digit digitSum is to be found.

6) Given an array of strings, group anagrams together. (String with the same
characters and same frequency, further order of characters can be different).

Example:

Input: ["eat", "tea", "tan", "ate", "nat", "bat"],


sorted string = [“aet”, “aet”, “ant”, “aet”, “ant”, “abt”]
Output:
[
["ate","eat","tea"],
["nat","tan"],
["bat"]
]
Note:

All inputs will be in lowercase.


The order of your output does not matter.

7) Battleship 8x8*

The purpose of this exercise is to train you to work with wrapper classes and their static
methods.

Estimated workload of this exercise is 3 hours.

Description

Please, implement methods in Battleship8x8 class.

Battleship8x8 represents a 8x8 map for Battleship game. An important detail is that
Battleship8x8 uses a ships field of long type to store ships locations and a shots field of long
type to register shots.
Fields of long type value store 64 bits each. Consider them as 8 rows per 8 cells. '0' bits
represent empty cells, '1' bits represent cells seized by ships or registered shots.
 public boolean shoot(String shot) - Registers a shot and returns true if the shot hits a
ship. A shot is a combination of one of A-H letters and one of 1-8 digits
 public String state() - Returns a string representing state of the map. Map string is 8
lines per 8 characters separated by "\n". Use following symbols:
o '.' - an empty cell

o '×' - an empty cell that has been shot


o '☐' - a cell seized by a ship
o '☒'- a cell seized by a ship that has been shot

You must not add or change new fields. Consider using static methods of wrapper classes.

Example

Consider a map information encoded as a long value:


long map = -1150950973573693440L;

You may consider it represented as binary literal:

/*
11110000

00000111

00000000

00110000

00000010

01000000

00000000

00000000

*/
long map = 0b11110000_00000111_00000000_00110000_00000010_01000
000_00000000_00000000L;

Consider rows be marked with digits and columns be marked with letters:
/*
ABCDEFGH

┌────────
1│11110000

2│00000111

3│00000000

4│00110000

5│00000010

6│01000000

7│00000000

8│00000000

*/
Consider the following list of shots:

List<> shots = List.of("A1", "B2", "C3", "D4");

Then we could use Battleship8x8 API:

Battleship8x8 battle = new Battleship8x8(map); shots.forEach(battle::shoot);


System.out.println(battle.state());

The result would be the following:

☒☐☐☐....

.×...☐☐☐

..×.....

..☐☒....

.........☐.

.☐......

........

........
8) Create an ArrayList with four string elements and perform the following operations:
9) Change the third element.
10) Remove the fourth element.
11) Display the total no. of elements.
12) Traverse all the elements using for-each loop.
13) Display all the elements after sorting.
14) Remove all the elements of the ArrayList.
15) Create a HashMap object with four key-value pairs and perform the following operations:
16) Access the second value.
17) Remove the third item.
18) Display the total no. of items.
19) Display the keys and values separately.

20) Demonstrate a program that implements the retrieval of elements from Set using
Iterator.

21) WAP to create ArrayList object with


22) 3 String objects and 3 Integer objects
23) While iterating replaces all string objects with uppercase characters
24) Insert the number 20 after the second integer object
25) 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 n^p . 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."
26) Write a Java program to create a method that takes an integer as a parameter and throws
an exception if the number is divisible by 5.
27) Write a Java program to create a method that takes a string as input and throws an
exception if the string contains “_” character.

28) WAP to check ArrayIndexOutOfBoundsException by trying to access an index of the array


outside of its boundary.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy