Coding Questions - Java
- Bhanu Uday
- Oct 14, 2021
- 3 min read
Hi, Hope you are doing well.
I share some easy problem coding questions.
1. Rotating chessboard [easy]
Description:
The game of Chess uses the following notation to label the squares on the grid: assuming White is below
and black above, the first row at the bottom is labeled from A1 to H1, the second row as A2 to H2 etc. upto the top-most row as A8 to H8.
We extend this notation to chess-boards of arbitrary sizes from 2x2 to 26x26 (although how one plays chess on this is an open mystery!).
The notation is the same as that for a standard 8x8 board e.g. a 4x4 board will be as such:
R4 | A4 B4 C4 D4
R3 | A3 B3 C3 D3
R2 | A2 B2 C2 D2
R1 | A1 B1 C1 D1
C1 C2 C3 C4
We want to see how the chess-board looks after rotating it in multiple turns. Each turn is either clock-wise or anti-clockwise in multiples of 90 degrees. Thus C 1 means a single clockwise turn of 90 degrees, A 5 means an anticlockwise turn of 5 * 90 = 450 degrees.
Given a chess board of a certain size, and a rotation command as above, you have to display the board
layout after the rotation.
E.g. for the 4x4 board above and a command of "A 5" the board layout would be:
R4 | D4 D3 D2 D1
R3 | C4 C3 C2 C1
R2 | B4 B3 B2 B1
R1 | A4 A3 A2 A1
C1 C2 C3 C4Input
The input will consist of the size of the board (length of each side) n, followed in the next line by the rotation command in the format C (clockwise) or A (anti-clockwise) followed by an integer r giving the number of (90 degree) turns.
Output
Should consist of n rows and n space separated entries in each row giving the label of each square in the rotated chessboard.
Sample input
4
A 5
Sample output
D4 D3 D2 D1
C4 C3 C2 C1
B4 B3 B2 B1
A4 A3 A2 A1Explanation
As described in the first section.
2. Painting boxes [easy]
Description
There are N boxes of colors R(ed) and B(lue) kept in a line. The boxes are in random order. You are allowed to repaint any of the boxes with the aim that after the repainting all the R boxes will be before the B boxes as observed from left to right. What is the minimum number of repaintings you need to do to ensure that the condition above is satisfied?
Input
N the number of boxes on the first line. A string of size N with the letters R and B denoting the two possible colors.
Output
A single integer giving the minimum number of repaintings needed to arrive at the final configuration.
Note: the answer can be 0 also.
Sample input
RYRYR
YRYRY
RRRRR
RRYYRYRY
Sample output
2
2
0
2Explanation
Case 1: Possible solutions: RRRRR, RYYYY, RRRYY
Case 2: Possible solutions: YYYYY, RRYYY, RRRRY
Case 3: Is already in required state so no changes needed
Case 4: Solution: RRYY3?YYYY
3. Traveling robot [easy]
Description
Ronnie the robot has escaped from home. He travels in a 2D world along the directions - East, West, North and South - using instructions which are fed to him in a script. The script is given in the form of a string of letters indicating the directions - E (East), W (West), N (North), S (South). He follows the script by reading each letter sequentially and moving in that direction by 1 unit. Unfortunately the script he has, has been corrupted, and some letters are not visible. The missing letters are indicated with a "?".
Can you help Ronnie get the farthest from home by filling in the missing letters in such a way that his 2D unitwise distance from home (say coordinate (0,0)) is maximized. The 2D unitwise distance between two points (x1,y1) and (x2,y2) is given by abs(x1-x2) + abs(y1-y2).
Input
The script in the form of a string of letters containing one of the following characters: 'E' , 'W' , 'N' , 'S' , '?'.
Output
A single integer indicating the maximum unit distance Ronnie can get from home (based on the
appropriate substitutions for the missing ('?') characters)
Sample input
EN?WW??NNSW?
Sample output
8Explanation
One possible way to reach this distance is by using the following to fill the missing
direction(instruction)s:
ENNWWWWNNNSWW
(Note: there may be other ways to fill the missing directions, but the maximum possible distance will be
8 in this case.)
If you need solution of these questions or looking any other java assignment help then you can contact with us and get instant help.



Comments