0% found this document useful (0 votes)
118 views22 pages

Programming Challenges

The document provides 18 programming challenges with sample inputs and expected outputs. The challenges include tasks like sorting student records by score, validating usernames, calculating distances traveled by a hopping frog, determining the day of the month for a given date, and more computational logic and string/number manipulation problems. Solutions to each challenge are requested.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
118 views22 pages

Programming Challenges

The document provides 18 programming challenges with sample inputs and expected outputs. The challenges include tasks like sorting student records by score, validating usernames, calculating distances traveled by a hopping frog, determining the day of the month for a given date, and more computational logic and string/number manipulation problems. Solutions to each challenge are requested.

Uploaded by

venkatesh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 22

Programming Challenges.

================================================================================

Programming Challenges Question - 1


Write a program that accepts 10 student
records (roll number and score) and prints
them in decreasing order of scores. In case
there are multiple records pertaining to
the same student, the program should choose
a single record containing the highest
score. The program should be capable of
accepting a multi-line input. Each
subsequent line of input will contain a
student record, that is, a roll number and
a score (separated by a hyphen). The output
should consist of the combination of roll
number and corresponding score in
decreasing order of score.
INPUT to program

1001-40
1002-50
1003-60
1002-80
1005-35
1005-55
1007-68
1009-99
1009-10
1004-89

OUTPUT from program


1009-99
1004-89
1002-80
1007-68
1003-60
1005-55
1001-40

Solution of Challenge - 1

Programming Challenges Question - 2


Sam wants to select a username in order to
register on a website.
The rules for selecting a username are:

1. The minimum length of the username must


be 5 characters and the maximum may be 10.
2. It should contain at least one letter
from A-Z
3. It should contain at least one digit
from 0-9
4. It should contain at least one character
from amongst @#*=
5. It should not contain any spaces

Write a program which accepts 4 usernames


(one username per line) as input and checks
whether each of them satisfy the above
mentioned conditions.
If a username satisfies the conditions, the
program should print PASS (in uppercase)
If a username fails the conditions, the
program should print FAIL (in uppercase)

Suppose the following usernames are


supplied to the program:
1234@a
ABC3a#@
1Ac@
ABC 3a#@

Then the output should be:


PASS
PASS
FAIL
FAIL

Solution of Challenge - 2

Programming Challenges Question - 3


Kermit, a frog hops in a particular way
such that:

1. He hops 20cm in the first hop, 10cm in


the second hop and 5cm in the third hop.
2. After three hops Kermit rests for a
while and then again follows the same
hopping pattern.

Calculate the total distance traveled by


Kermit (in centimeters) for the provided
number of hops. Exactly 4 numbers of hops
will be provided to the program (one number
per line) as per the below example.

Suppose the following number of hops is


provided to the program:
4
6
3
5

Then the total distance covered should be


displayed as follows:
55
70
35
65

Solution of Challenge - 3

Programming Challenges Question - 4


Write a program which will accept a single
pair of strings separated by a comma; the
program should calculate the sum of ascii
values of the characters of each string.
The program should then subtract the sum of
the ascii values of the second string from
the sum of the ascii values of the first
string.
Suppose the following input is given to the
program:

123ABC,456DEF
Then the sum of the ascii values of the
characters in '123ABC' is 348 and in
'456DEF' it is 366. The Difference between
these numbers is 348 – 366 = -18

The corresponding output to be printed by


the program is:

-18
Solution of Challenge - 4

Programming Challenges Question - 5


Write a program which will take the year
(yyyy) and the numeric sequence of the
month (0-11) as its input. The program will
return the day on which the 28th of that
particular month and year falls. The input
can consist of two year-month combinations,
one combination per line.

The numeric sequence of months is as


follows:

0 – Jan
1 – Feb
2 – March
and so on......
The format for supplying the input is:

1999-5

Where 1999 is the year and 5 is the numeric


sequence of the month (corresponding to
June). The program should display the day
on which June 28, 1999 fell, and in this
case the output will be MONDAY.

The output should be displayed in uppercase


letters.

Suppose the following INPUT sequence is


given to the program:

1999-5
1998-6
Then the output should be:

MONDAY
TUESDAY
Solution of Challenge - 5

Programming Challenges Question - 6


Write a program which will accept three
sentences (one sentence per line) and print
the words having Initial Caps within the
sentences. Below is an example.
Here is an example. If the below three
sentences are given to the program as

input,

This is a Program
Coding test of Initial Caps
the program Will Test You
Then, the output would look like:

This
Program
Coding
Initial
Caps
Will
Test
You
Solution of Challenge - 6

Programming Challenges Question - 7


Write a program which will print the below
structures according to the input provided
to the program. The program should accept 3
inputs in the form of numbers between 1 and
9, both inclusive (one number per line) and
then generate the corresponding structures
based on the input.

Suppose the following sequence of numbers


is supplied to the program:
3
2
4
Then the output should be:

1
2 2
3 3 3

1
2 2

1
2 2
3 3 3
4 4 4 4

Solution of Challenge - 7

Programming Challenges Question - 8


Write a Java program to reverse a String in
given particular way without creating any
new String object.

Input- "Kushwaha Umesh is name My"

Output- "My name is Umesh Kushwaha"


Solution of Challenge - 8

Programming Challenges Question - 9


Write a method that given a list of non
negative integers, arranges them such that
they form the largest possible number. For
example, given [50, 2, 1, 9], the largest
formed number is 95021.

Input- [62, 5, 1, 3]

Output- 62531

Solution of Challenge - 9

Programming Challenges Question - 10


Write a program that outputs all
possibilities to put + or - or nothing
between the numbers 1, 2, ..., 9 (in this
order) such that the result is always 100.

For example: 1 + 2 + 34 – 5 + 67 – 8 + 9 =
100.

Input- TARGET_SUM = 100;

VALUES = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

Output-
1+2+3-4+5+6+78+9
1+2+34-5+67-8+9
1+23-4+5+6+78-9
1+23-4+56+7+8+9
12+3+4+5-6-7+89
12+3-4+5+67+8+9
12-3-4+5-6+7+89
123+4-5+67-89
123+45-67+8-9
123-4-5-6-7+8-9
123-45-67+89

Solution of Challenge - 10

Programming Challenges Question - 11


Write a program that accepts a comma
separated string of numbers depicting the
daily stock price of AXY Company. The
program should calculate the biggest daily
fluctuation (in terms of percentage) of the
stock price on any given day.

For example, let us suppose the following


is the input string:

INPUT
20,23,25,30,22,21,16
Then the corresponding biggest fluctuation
is:
OUTPUT
26.6666666666667 (As the maximum
fluctuation in price is between 22 and 30)
Solution of Challenge - 11

Programming Challenges Question - 12


Write a program that accepts 4 words as
input (one per line) and identifies whether
they are palindromes. A palindrome is a
word that reads the same when read back to
front. The program should print the
alphabetically sorted list of palindromes
(one per line). The program output should
be in lowercase.
INPUT
waas
gaag
haaha
ABBA

OUTPUT
abba
gaag

Solution of Challenge - 12

Programming Challenges Question - 13


Write a program which will take the volume
of the cube as its input. The program
should calculate the area of a square whose
side is half the length of the side of the
cube. The program should print the area of
the square.

Definitions:
Volume of cube = aaa, where a is the side
of a cube
Area of Square = b*b, where b is the side
of the square

For example, let us suppose the following


is the volume inputted to the program:
INPUT
8
27
Then the corresponding output will be as
follows:
OUTPUT
1
2.25

Solution of Challenge - 13

Programming Challenges Question - 14


Write a program which will accept a number
as its input. You are required to calculate
the sum of the first and last digits of the
square of the number given as inp ut. In
case the square of the number is a single
digit number then the square should be
printed as it is.
For example, let us suppose the following
input is given to the program:
INPUT3 5 6
Then the corresponding output should be:
OUTPUT9 7 9
Solution of Challenge - 14

Programming Challenges Question - 15


Write a program that accepts a number of
ASCII values between 40 and 125 (both
inclusive) and concatenates the
corresponding characters to form a string.
The progra m accepts a multi-line input.
The first line contains a number
representing how many ASCII values are in
the input. The subsequent lines contain the
ASCII values th emselves (one ASCII value
per line).

The following is an example of a valid


input:

INPUT5 (this indicates the total number of


ASCII values in the input set) 65 67 69 70
71

The output is printed as a string. The


ASCII value of A is 65, C is 67, E is 69, F
is 70, G is 71 and therefore the output
will be as follows:
OUTPUT
ACEFG

Solution of Challenge - 15

Programming Challenges Question - 16


During a car rally the time keeper wrote
down the driver names along with their
finishing positions in random order. Write
a program to sort the names of the driver s
in ascending order of their finishing
positions.

For example, let us suppose the following


input is given to the program:

INPUTEddie-3 Michael-1 Nica-4 Montoya-6

Then the program should print the output as


follows:

OUTPUTMichael-1 Eddie-3 Nica-4 Montoya-6

Solution of Challenge - 16

Programming Challenges Question - 17


Write a program which will accept a set of
numbers. For each number, it will calculate
the sum of the cubes for the first 'n'
natural numbers counting up to that nu
mber. The first line of the input should
consist of a number representing how many
numbers are in the input set. The
subsequent lines contain the numbers
themselves (one per line).
Here is an example of how the input and
output would look:

INPUT3 (this indicates the total count of


numbers in the input set) 1 2 4

OUTPUT1 9 100

Solution of Challenge - 17

Programming Challenges Question - 18


A bank requires its clients to change their
internet banking passwords based on the
following conditions:
Alphabets between A-Z or a-z (inclusive)
are allowed

The number of alphabets should not be less


than 4

Numbers between 0-9 (inclusive) are allowed


The count of numbers in the password should
not be less than 2

Write a program to validate the internet


banking password for the above conditions.
If the password meets the conditions then
"VALID" should be printed, else "INVAL ID"
should be printed.

INPUTa2 abcdEF12 abcdEF12(*

OUTPUTINVALID VALID VALID


Solution of Challenge - 18

Programming Challenges Question - 19


Write a program that evaluates the sum of
all the divisors of a given number, other
than the number itself. For example, if the
number if 14, the divisors are 1,2,7 and
14. If we exclude 14, the sum of the
remaining divisors will be 1+2+7 = 10.
The program accepts a multi-line input. The
first line contains a number representing
how many numbers are being inputted.
Subsequent lines contain the numbers them
selves. The inputted numbers should be
between 1 and 100000 (inclusive). The
output should be printed one per line
corresponding to the sum of the divisors of
the p articular inputted number.
Below is some example input and output:

INPUT4 (this represents how many numbers


exist in the input set) 13 15 10 6

OUTPUT1 9 8 6

Note: In case of input data being supplied


to the question, it should be assumed to be
a console input.

Solution of Challenge - 19

Programming Challenges Question - 20


Write a program which will convert the
given RGB input values into the
corresponding hexadecimal values preceded
with a # sign.

A single input sequence will be provided,


consisting of RGB values separated by "-".
Each RGB combination will be separated by
",". The outputs should be comma sepa rated
and in uppercase. Also, it should be
checked if a color value is greater than
255. In such a case, the output for the
corresponding combination should be give n
as INVALID.

Note: Number 10, which has a hexadecimal


value of A, must be represented as "0A" and
not as "A". The same rule applies to other
single digit hexadecimal numbers.
Example
Suppose the following input sequence is
supplied to the program:

INPUT12-13-14,45-156-23,234-234-256
OUTPUT#0C0D0E,#2D9C17,INVALID

Solution of Challenge - 20

Programming Challenges Question - 21


Write a program to calculate the bill
amount, in cents, for the units of power
consumed. Following are the rates
applicable:
First 0-100 units: 60 cents per unit
Next 200 units: 70 cents per unit
Beyond 300 units: 80 cents per unit

The program should accept three different


usage unit readings.

Example

If the following inputs are supplied:

INPUT305 180 120

Then, the output should be:

OUTPUT20400 11600 7400

Solution of Challenge - 21

Programming Challenges Question - 22


Ross is an event organizer. He has received
data regarding the participation of
employees in two different events. Some
employees have participated in only one
event and others have participated in both
events. Ross now needs to count the number
of employees who have taken part in both
events. The records received by Ross
consist of employee ids, which are unique.
Write a program that accepts the employee
ids participating in each event (the first
line relates to the first event and the
second line relates to the second event).
The program should print the number of
common employee ids in both the events.

INPUT1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112

Now the common employee ids are 1003 and


1005, so the program should give the output
as:
OUTPUT2
Solution of Challenge - 22

Programming Challenges Question - 23


Write a program to calculate the distance
travelled by a car at different time
intervals. The initial velocity of the car
is 36 km/hr and the constant acceleration
is 5 m/s2.
The formula to calculate distance is:
Distance Travelled = ut+((at*t)/2) where, u
= initial velocity of the car (36 km/hr) a
= acceleration of the car (5 m/s2) t = time
duration in seconds

The program should accept 2 time intervals


as the input (one time interval per line)
and print the distance travelled in meters
by the car (one output per line).

Definitions: 1 kilometer = 1000 meters 1


hour = 3600 seconds
Let us suppose following are the inputs
supplied to the program
INPUT10 8
OUTPUT350 240
Solution of Challenge - 23

Programming Challenges Question - 24


Write a program that accepts 5 sentences as
input (one sentence per line) and counts
the number of vowels (a,e,i,o,u,A,E,I,O,U)
occurring in each sentence. The program
should print the vowel count corresponding
to each of the five sentences as shown in
the below example.

INPUTThis is a test The Fox and the Hound


May the force be with you Are you talking
to me Go West
OUTPUT4 6 8 8 2
Solution of Challenge - 24

Programming Challenges Question - 25


Write a program which prints the numbers
between 2 and 20000 (both inclusive) whose
sixth root is an integer and which are a
square of a number and also a cube of a
number. An example of such a number is 64
since its sixth root is 2. 64 is also the
square of 8 and cube of 4.

OUTPUTNum1 Num2 Num3

Solution of Challenge - 25

Programming Challenges Question - 26


Write a program which will validate four
inputted ip addresses (one per line). A
valid ip address must follow the form
xxx.xxx.xxx.xxx where xxx is a number from
0-255. The program should print "VALID" if
the ip address is valid, and "INVALID" if
the ip address is invalid.
Example:
INPUT12.12.13.14. 256.245.234.221 1.2.3.4
23.24.25.26.

OUTPUTINVALID INVALID VALID INVALID

Solution of Challenge - 26
Programming Challenges Question - 27
Write a program which accepts 4 integer
values (one per line) and converts them to
their corresponding 16 bit binary values.
The output should be printed one value per
line.

INPUT8 10 17 23

OUTPUT0000000000001000 0000000000001010
0000000000010001 0000000000010111

Solution of Challenge - 27

Programming Challenges Question - 28


Write a program that accepts four dates
(one per line) and checks to see if they
are in the date format "mm-dd-yyyy" (month-
day-year). If the date is found to be in
the valid format, the program should print
"VALID", if not, the program should print
"INVALID".Example:
INPUT01-01-1999 13-11-1999 10-30-1999

OUTPUTVALID INVALID VALID

Solution of Challenge - 28

You might also like