Coding Questions
Coding Questions
Question 1: (Moderate)
Imagine you are developing a simple utility program for a local electricity
provider.
The provider wants a tool to calculate the electricity bill for its customers based
on their monthly usage. The billing system operates on a tiered pricing structure
where different rates apply for different levels of electricity consumption.
Your program allows the provider's staff to input the total units of electricity
consumed by a customer during the billing period.
For the first 50 units, the rate is Rs. 0.50 per unit.
For the next 100 units (51 to 150), the rate is Rs. 0.75 per unit.
For the next 100 units (151 to 250), the rate is Rs. 1.20 per unit.
For any units above 250, the rate is Rs. 1.50 per unit.
Your program simplifies the billing process, ensuring accurate and efficient
billing for the electricity provider and providing transparency to their customers
about the charges they incur based on their electricity usage.
Input Format
Output Format
Print the Amount to be paid by the customer for the units used by them(with 2
decimal places).
Test Case 1
Input
40
Output
25.00
Test Case 2
Input
55
Output
35.94
Test Case 3
Input
156
Output
134.00
Code:
#include <stdio.h>
int main() {
int units;
scanf("%d", &units);
} else {
amount = (50 * 0.50) + (100 * 0.75) + (100 * 1.20) + ((units - 250) * 1.50
);
printf("%.2f", total_amount);
return 0;
Question 2: (Moderate)
Triangle Game
The Westland Game Fair is the premier event of its kind for kids interested in some
intellectual and cognitive brain games. Exciting games were organized for kids
between the ages group of 8 and 10. One such game was called the "Triangle
game", where different number boards in the range of 1 to 180 are available. Each
kid needs to select three number boards, where the numbers on the boards
correspond to the angles of a triangle.
If the angles selected by a kid form a triangle, he/she would receive Prize 1. If the
angles selected by a kid form a right triangle, he/she would receive Prize 2. If the
angles selected by the kids form an equilateral triangle, he/she would receive Prize
3. If the angles selected by a kid do not form even a triangle, then he/she will not
receive any prizes. Write a program for the organizers to fetch the result based on
the number of boards selected by the kids.
Input Format
There are 3 lines in the input, each of which corresponds to the numbers on the
boards that the kids select.
Output Format
The output should display "Prize 1" or "Prize 2" or "Prize 3" or "No Prize" based
on the conditions given.
Refer sample input and output for formatting specifications.
Test Case 1
Input
50
60
70
Output
Prize 1
Test Case 2
Input
60
60
70
Output
No Prize
Test Case 3
Input
60
60
60
Output
Prize 3
Code:
#include <stdio.h>
int main() {
scanf("%d", &angle1);
scanf("%d", &angle2);
scanf("%d", &angle3);
printf("Prize 3\n");
else {
printf("Prize 1\n");
} else {
printf("No Prize\n");
return 0;
Question 3: (Easy)
Please help him by giving a command by telling him the direction in which he
should go, so as to reach his home. If you give him a direction, he will keep
moving in that direction till he reaches home. There are four possible directions
you can give as a command - "left", "right", "up", or "down". It might be possible
that you can't instruct Harry in such a way that he reaches his home. In that case,
display the output as "sad".
Input Format
The first line of the input contains four space-separated integers x1, y1, x2, and y2.
Output Format
Output a single line containing "Left" or "Right" or "Up" or "Down" or "Sad"
(without quotes).
Refer sample input and output for formatting specifications.
Test Case 1
Input
0010
Output
Right
Test Case 2
Input
0011
Output
Sad
Code:
#include <stdio.h>
int main() {
printf("Up\n");
} else {
printf("Down\n");
printf("Right\n");
} else {
printf("Left\n");
} else {
printf("Sad\n");
return 0;
Question 4: (Moderate)
Chocolate Game
It was Christmas Eve and the celebrations remembering the birth of Jesus were
going on in full swing at the Cathedral Chapel. The Event Management Team had
arranged for some exciting games after the mass worship and feast, where adults
and kids of all ages participated very actively. "Chocolate Game" was organized
for the kids which involved standard chocolate of n by m pieces. More formally,
chocolate is in a rectangular plate consisting of n rows and m columns.
Two kids at a moment will play with the chocolate. The first kid takes the
chocolate and cuts it into two parts by making either a horizontal or vertical cut.
Then, the second kid takes one of the available pieces and divides it into two parts
by either making a horizontal or vertical cut. Then the turn of the first kid comes
and he can pick any block of the available chocolates and do the same thing again.
The player who cannot make a turn loses.
Write a program to find which of the kids will win if both of them play optimally.
Output "Yes", if the kid who plays first will win, otherwise print "No".
Input Format
The first line of input should be the size of n.
The second line of input should be the size of m.
Output Format
Output a single line containing the word "Yes" (without quotes) if there is a
sequence of moves leading to the winning of the person who moves first and "No"
(without quotes) otherwise.
12
Output
Yes
Test Case 2
Input
13
Output
No
Code:
#include <stdio.h>
int main() {
int n, m;
scanf("%d", &n);
scanf("%d", &m);
if (n *m)%2==0 {
printf("Yes\n");
} else {
printf("No\n");
return 0;
}
Question 1: (Easy)
Write a program to print the sum of 1 to n. While calculating the sum omit the number
which is multiple of x
Input Format
The first line of the input consists of an integer n.
The second line of the input consists of an integer x.
Output Format
The output prints the sum of elements.
Test Case 1
Input
50
6
Output
1059
Test Case 2
Input
20
2
Output
100
Test Case 3
Input
120
4
Output
5400
Solution:
In C Language:
#include <stdio.h>
int main()
{
int n,x,sum=0;
scanf("%d",&n);
scanf("%d",&x);
for(int i=1;i<=n;i++){
if(i%x!=0){
sum+=i;
}
}
printf("%d",sum);
return 0;
}
Question 2: (Easy)
Given a number N print a right-angled triangle structure with the starting level as single 1
and every immediate proceeding level with 2 more additional ones than the previous
level. Repeat the pattern for N levels.
Note: Ensure no spaces at the end of each line.
Input format :
The input contains integer N which represents pattern length
Output format :
Output displays right-angled triangle formed by '1' with the starting level as single 1 and
every immediate proceeding level with 2 more additional ones than the previous level for
N times
Test Case 1
Input
5
Output
1
111
11111
1111111
111111111
Test Case 2
Input
7
Output
1
111
11111
1111111
111111111
11111111111
1111111111111
Solution :
In C Language:
#include <stdio.h>
int main()
{
int row,column=1;
scanf("%d",&row);
for(int i=1;i<=row;i++){
for(int j=1;j<column;j++){
printf("1 ");
}
printf("1\n");
column+=2;
}
return 0;
}
Question 3: (Moderate)
Hotel Royal Gardenia has arranged for an elite business party for the lead industrialists
and celebrities of the City. Followed by a dinner buffet, the Event coordinators planned
for some casino game events for the high-toned crowd. Peter was a visitor at the party
and he takes some rubles to the casino with the intention of becoming rich. He plays
three machines in turn. Unknown to him, the machines are entirely predictable. Each play
costs one ruble. The first machine pays 20 rubles every 25th time it is played; the second
machine pays 80 rubles every 120th time it is played; the third pays 8 rubles every 12th
time it is played.
Given the number of rubles with Peter initially (there will be at least one and fewer than
1000), and the number of times each machine has been played since it last paid, write a
program that calculates the number of times Peter plays until he goes broke.
Input format :
The first line of the input is an integer that corresponds to the number of rubles with
Peter initially.
The next 3 lines of the input an integer that corresponds to the number of times each
machine has been played since it last paid.
Output format :
Output a single line that gives the number of times Peter plays until he goes broke.
Refer sample input and output for formatting specifications.
Input 2 :
35
10
30
9
Output 1 :
71 times
Solution:
In C Language:
#include <stdio.h>
int main()
{
int amount,m1,m2,m3,play=0;
scanf("%d",&amount);
scanf("%d",&m1);
scanf("%d",&m2);
scanf("%d",&m3);
while(amount>0){
m1++;
play++;
amount--;
if(m1%25==0){
amount+=20;
}
if(amount>0){m2++;
play++;
amount--;
}
else
break;
if(m2%120==0){
amount+=80;
}
if(amount>0){
m3++;
play++;
amount--;
}
else
break;
if(m3%12==0){
amount+=8;
}
}
printf("%d times",play);
return 0;
}
Input format:
Read an Integer N
Output format:
Example:
N=121
output: 4 (1+2+1)
Test Case 1:
Input:
1689
Output:
24
Test Case 2:
Input:
523
Output:
10
Solution:
#include <stdio.h>
int main()
{
int n,sum=0;
scanf("%d",&n);
while(n>0){
sum+=(n%10);
n/=10;
}
printf("%d",sum);
return 0;
}
Question 1
Input Format
First line of input denotes Integer N which denotes the number of data for the array.
Second line of input denotes the datas in the array.
Output Format
Print the Maximum Number in a array
Test Case 1
Input
3
175
Output
7
Test Case 2
Input
6
20 21 58 36 74 59
Output
74
Test Case 3
Input
4
105 78 535 144
Output
535
Test Case 4
Input
6
260 281 528 326 474 359
Output
528
Test Case 5
Input
5
145 37 59 74 104
Output
7145
Solution:
In C Language:
#include <stdio.h>
int main()
{
int n,max;
scanf("%d",&n);
int a[n];
for (int i=0;i<n;i++){
scanf("%d",&a[i]);
}
max=a[0];
for (int i=1;i<n;i++){
if(max<a[i])
max=a[i];
}
printf("%d",max);
return 0;
}
-----------------------------------------------------------------------------------------
Question 2
Output Format
Print the sum of odd numbers present in the array
Test Case 1
Input
5
23456
Output
8
Test Case 2
Input
7
2345678
Output
15
Test Case 3
Input
5
22 35 46 52 63
Output
98
Test Case 4
Input
6
24 32 46 52 68 28
Output
0
Test Case 5
Input
4
23 36 41 54 61
Output
125
Solution:
In C Language:
#include <stdio.h>
int main()
{
int n,sum=0;
scanf("%d",&n);
int a[n];
for (int i=0;i<n;i++){
scanf("%d",&a[i]);
if(a[i]%2==1)
sum+=a[i];
}
printf("%d",sum);
return 0;
}
----------------------------------------------------------------------------------------------------------
Question 3
Count of Elements
N is an integer.
Read an array and Count number of integer M present in the array.
Input Format
First line of input denotes an integer N
Second line of input denotes the data present in the array
Last line of input denotes the integer M that you have to count
Output Format
Print the count of the Integer in the array
Test Case 1
Input
5
13 16 25 13 14
13
Output
2
Test Case 2
Input
4
7 8 24 12
45
Output
0
Test Case 3
Input
6
23 16 23 23 14 26
23
Output
3
Test Case 4
Input
4
7 8 2 12
12
Output
1
Test Case 5
Input
5
23 46 25 53 14
13
Output
0
Solution:
In C Language:
#include <stdio.h>
int main()
{
int n,b,count=0;
scanf("%d",&n);
int a[n];
for (int i=0;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&b);
for (int i=0;i<n;i++){
if(a[i]==b)
count++;
}
printf("%d",count);
return 0;
}
—---------------------------------------------------------------------------------------------
Question 4
Square Pattern
Read a Integer N that denotes no of rows and columns in the square pattern
Input Format
Read an Integer N.
Output Format
Print the square pattern based on the Integer N
Test Case 1
Input
5
Output
*****
* *
* *
* *
*****
Test Case 2
Input
4
Output
****
* *
* *
****
Solution
In C Language:
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if (i==1|| i==n || j==1||j==n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Question 01:
Problem Statement:
Change the case of a String by using ASCII Values.
if the String is in Upper case means change to Lower case else vice versa.
Input Format:
String S
Output Format:
Print the string after changing the case
Sample Input 1:
Hello
Sample Output 1:
hELLO
Sample Input 2:
Good Morning
Sample Output 2:
gOOD mORNING
Solution:
In C Language:
#include <stdio.h>
int main()
{
char str[100];
scanf("%[^\n]s",str);
for(int i=0;str[i]!='\0';i++){
if(str[i]>='A' && str[i]<='Z')
printf("%c",(str[i]+32));
else if(str[i]==32)
printf(" ");
else
printf("%c",(str[i]-32));
}
return 0;
}
Question 02:
Happy String
Problem Statement:
Chef is happy if the string contains greater than 2 characters are vowels.
Input Format:
A single line of input, a string S
Output Format:
If Chef is HAPPY, print HAPPY else print SAD.
Sample Input 1:
Aeiou
Sample Output 1:
Happy
Sample Input 2:
Hello
Sample Output 2:
Sad
Solution:
In C Language:
#include <stdio.h>
int main()
{
char str[100];
int c=0;
scanf("%s",str);
for(int i=0;str[i]!='\0';i++){
if(str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U' || str[i]=='a' || str[i]=='e' || str[i]=='i'
|| str[i]=='o' || str[i]=='u')
c++;
}
if(c>2)
printf("Happy");
else
printf("Sad");
return 0;
}
Question 03:
Problem Statement:
Read an array by reading number of rows (Integer M) and number of columns (Integer N).
Read a Column Number as Integer A.
Sum the data in Column A.
Input Format:
First line of input denotes No. of rows M.
Second Line of input denotes No. of columns N.
From third Line data in the array
Last line of Input denotes the Column Number (Integer A) that you are going to sum.
Output Format:
Print the sum of the column A.
Sample Input 1:
2
2
1234
1
Sample Output 1:
4
Sample Input 2:
3
3
123456789
2
Sample Output 2:
15
Solution :
In C Language:
#include <stdio.h>
int main()
{
int r,c,n,sum=0;
scanf("%d%d",&r,&c);
int a[r][c];
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
scanf("%d",&a[i][j]);
}
}
scanf("%d",&n);
for(int i=0;i<r;i++){
sum+=a[i][n-1];
}
printf("%d",sum);
return 0;
}
Question 04:
Search Element
N is a integer denotes number of elemets for the array.
Read an array.
Search the given Element M in the array.
Input Format
First line of input denotes Integer N which denotes the number of elements for the array
Second line of input denotes the datas present in the array
Third line of input denotes the element M that we get from the user
Output Format
Print "Yes" or "No" Element is element in the array
Test Case 1
Input
5
23 45 67 89 100
100
Output
Yes
Test Case 2
Input
7
89 66 77 45 34 90 67
1000
Output
No
Solution :
In C Language:
#include <stdio.h>
int main()
{
int n,ele,c=0;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&ele);
for(int i=0;i<n;i++){
if(a[i]==ele)
c++;
}
if(c!=0)
printf("Yes");
else
printf("No");
return 0;
}
Question 1:
Chef loves integers that are in the range of l to r.
More formally, Chef loves an integer x if it satisfies the condition l ≤ x ≤ r.
Chef has an array A of length N. Currently, the happiness of Chef is 0.
He will examine the elements of the array in order from index 1 to N.
If he finds an integer that he loves, his happiness will increase by 1; otherwise, his happiness
will decrease by 1.
Find the values of maximum and minimum happiness Chef will experience while going through
the array.
Input Format:
The first line of input will contain a single integer T, denoting the number of elements in the
array
Output Format:
Input:
4
4321
13
Output:
2 -1
Input:
7
10 11 9 19 21 15 20
10 20
Output:
31
Question 2:
Write a program to calculate the total bill tax amount for a list of billing amounts passed as an
array of long integers.
Up to the amount 1000, there is no tax applicable, subsequently, a flat tax of 10% is applicable
for the remaining amount as per the tax rate.
Note:
1. All calculations and results should be integer-based - ignoring fractions
2. You are expected to write code in the calcTotalTax function only which will receive the first
parameter as the number of items in the array and second parameter as the array itself. You are
not required to take input from the console.
Example:
Calculating total tax for a list of 5 billing amount
Input:
input1: 5
input2: 1000 2000 3000 4000 5000
Output:
1000
Input Format
First line of input denotes the size of the array.
Second line denotes the array of billing amounts.
Output Format
Find the Sum of all the tax amounts.
Test Case 1
Input
5
1000 2000 3000 4000 5000
Output
1000
Test Case 2
Input
5
500 1200 800 1080 2000
Output
128
Solution:
In Java:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int n1,sum=0;
n1=x.nextInt();
int[] a=new int[n1];
for (int i=0;i<n1;i++){
a[i]=x.nextInt();
}
for (int i=0;i<n1;i++){
if(a[i]>1000)
sum+=(a[i]-1000)*10/100;
}
System.out.println(sum);
}
}
Question 3:
You won’t get caught if you hide behind someone.”
Sang-Woo advises Gi-Hun to hide behind someone to avoid getting shot.
Gi-Hun follows Sang-Woo's advice and hides behind Ali, who saved his life earlier. Gi-Hun and
Ali both have the same height, K. Many players saw this trick and also started hiding behind Ali.
Now, there are N players standing between Gi-Hun and Ali in a straight line, with the ith player
having height Hi. Gi-Hun wants to know the minimum number of players who need to get shot
so that Ali is visible in his line of sight.
Note:
• Line of sight is a straight line drawn between the topmost point of two objects. Ali is visible to
Gi-Hun if nobody between them crosses this line.
• Even if there are some players who have the same height as that of Gi-Hun and Ali, Ali will be
visible in Gi-Hun's line of sight.
• Gi-Hun and Ali have the same height.
Input Format
The first line contains two space-separated integers N and K, denoting the total number of
players between Gi-Hun and Ali and the height of both of them respectively.
The second line of each test case contains N space-separated integers, denoting the heights of
the players between Gi-Hun and Ali.
Output Format
For each test case, output in a single line the minimum number of players who need to get shot
so that Ali is visible in Gi-Hun's line of sight.
Test Case 1
Input
4 10
2 13 4 16
Output
2
Test Case 2
Input
46
1234
Output
0
Solution:
In Java:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int n1,H,count=0;
n1=x.nextInt();
H=x.nextInt();
int[] a=new int[n1];
for (int i=0;i<n1;i++){
a[i]=x.nextInt();
}
for (int i=0;i<n1;i++){
if(a[i]>H)
count++;
}
System.out.println(count);
}
}
Question 4:
You are given an integer array height of length n. There are n vertical lines drawn such that the
two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the
most water.
Example 1:
Input:
9
186254837
Output:
49
Input:
2
11
Output:
1
Question 01:
Input Format:
String S
Output Format:
Print the string after changing the case
Sample Input 1:
Hello
Sample Output 1:
hELLO
Sample Input 2:
Good Morning
Sample Output 2:
gOOD mORNING
Sample Input 3:
programming
Sample Output 3:
PROGRAMMING
Question 02:
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed.
All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one.
Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two
adjacent houses were broken into on the same night.
Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can
rob tonight without alerting the police.
Input Format:
First Line of input denotes the no. of houses in the street.
Second line of input denotes an array representing the amount of money of each house.
Output Format:
The maximum amount of money you can rob tonight without alerting the police.
Example 1:
Input: nums = [2,3,2]
Output: 3
Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
Example 2:
Input: nums = [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
Total amount you can rob = 1 + 3 = 4.
Example 3:
Input: nums = [1,2,3]
Output: 3
Constraints:
Input:
3
2 3 2
Output:
3
Input:
4
1 2 3 1
Output:
4
Input:
3
2 3 6
Output:
6
import java.util.Scanner;
System.out.println(rob(nums));
}
Given a string s consisting of words and spaces, return the length of the last word in the string.
Input:
Hello World
Output:
5
Input:
fly me to the moon
Output:
4
Input:
Good Morning
Output:
7
Question 04:
A Company is transmitting data to another server. The data is in the form of numbers. To secure the data during
transmission, they plan to obtain a security key that will be sent along with the data. The security key is identified
as the count of the unique repeating digits in the data.
Input Format:
The input consists of an integer- data, representing the data to be transmitted.
Output Format:
Print an integer representing the security key for the given data.
Example
Input:
5783789236
Output
3
Explanation
The repeated digits in the data are 7,8 and 3. So, the security key is 3.
Input:
5783789236
Output:
3
Input:
5643278451
Output:
2
Input:
7823451679
Output:
1
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
if (Character.isDigit(digit)) {
int repeatingDigitCount = 0;
repeatingDigitCount++;
System.out.println(repeatingDigitCount);
Question 01:
You are given a string s. The score of a string is defined as the sum of the absolute difference
between the ASCII values of adjacent characters.
Example 1:
Input: s = "hello"
Output: 13
Explanation:
The ASCII values of the characters in s are: 'h' = 104, 'e' = 101, 'l' = 108, 'o' = 111. So, the score
of s would be |104 - 101| + |101 - 108| + |108 - 108| + |108 - 111| = 3 + 7 + 0 + 3 = 13.
Input
hello
Output
13
Input
zaz
output
50
Input
google
output
28
Solution:
In java:
import java.util.Scanner;
import java.lang.Math;
class Solution {
public static int scoreOfString(String s) {
int sum=0;
for(int i=0;i<s.length()-1;i++){
sum+=Math.abs(s.charAt(i)-s.charAt(i+1));
}
return sum;
}
}
public class Main{
public static void main(String[] args){
Scanner x=new Scanner(System.in);
String s=x.next();
Solution y=new Solution();
System.out.println(y.scoreOfString(s));
}
}
You are given a 0-indexed array of strings details. Each element of details provides information
about a given passenger compressed into a string of length 15. The system is such that:
Test Case:01
Input:
7868190130M7522 5303914400F9211 9273338290F4010
Output:
2
Input:
1313579440F2036 2921522980M5644
Output:
0
Solution:
class Solution {
public static int countSeniors(String[] details) {
int count = 0;
for(int i = 0; i<details.length; i++){
String passenger = details[i];
int num = Integer.parseInt(passenger.substring(11,13));
if(num>60){
count++;
}
}
return count;
}
Given an integer array nums, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Constraints:
Input:
1234567
3
Output:
567123
Solution :
In Java:
import java.util.*;
class Solution {
public static void rotate(int[] nums, int k) {
while(k>0){
int temp=nums[nums.length-1];
for(int i=nums.length-2;i>=0;i--){
nums[i+1]=nums[i];
}
nums[0]=temp;
k--;
}
for(int i=0;i<n;i++){
System.out.print(nums[i]+" ");
}
}
public static void main(String[] args){
Scanner x=new Scanner(System.in);
int n=x.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=x.nextInt();
}
int k=x.nextInt();
rotate(a,k);
}
}
4. Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the
string that you have written. Typing other characters works as expected.
You are given a 0-indexed string s, and you type each character of s using your faulty keyboard.
Return the final string that will be present on your laptop screen.
Example 1:
Input: s = "string"
Output: "rtsng"
Explanation:
After typing first character, the text on the screen is "s".
After the second character, the text is "st".
After the third character, the text is "str".
Since the fourth character is an 'i', the text gets reversed and becomes "rts".
After the fifth character, the text is "rtsn".
After the sixth character, the text is "rtsng".
Therefore, we return "rtsng".
Example 2:
Input: s = "poiinter"
Output: "ponter"
Explanation:
After the first character, the text on the screen is "p".
After the second character, the text is "po".
Since the third character you type is an 'i', the text gets reversed and becomes "op".
Since the fourth character you type is an 'i', the text gets reversed and becomes "po".
After the fifth character, the text is "pon".
After the sixth character, the text is "pont".
After the seventh character, the text is "ponte".
After the eighth character, the text is "ponter".
Therefore, we return "ponter".
Constraints:
Input:
string
Output:
rtsng
Input:
poiinter
Output:
ponter
Solution:
In Java Language:
import java.util.Scanner;
class Solution {
public static String finalString(String s) {
StringBuilder x=new StringBuilder("");
for(int i=0;i<s.length();i++){
char y=s.charAt(i);
if(y=='i'){
x.reverse();
}
else
x.append(Character.toString(y));
}
String str=x.toString();
return str;
}
public static void main(String[] args){
Scanner z=new Scanner (System.in);
String s=z.nextLine();
System.out.print(finalString(s));
}
}