0% found this document useful (0 votes)
85 views12 pages

Notes For Coding Test: 1 Purpose of These Notes

This document provides coding solutions in C, C++, Java and Python to various problems such as finding the largest of three numbers, calculating the digital root of a number, checking if all digits of a number are even, determining if a number is an Armstrong number, generating the Kaprekar constant sequence for a 4-digit number, and checking if a number is perfect. The solutions demonstrate good coding style and uses of language constructs. Quick reference guides for the languages are also included.

Uploaded by

It's VSP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views12 pages

Notes For Coding Test: 1 Purpose of These Notes

This document provides coding solutions in C, C++, Java and Python to various problems such as finding the largest of three numbers, calculating the digital root of a number, checking if all digits of a number are even, determining if a number is an Armstrong number, generating the Kaprekar constant sequence for a 4-digit number, and checking if a number is perfect. The solutions demonstrate good coding style and uses of language constructs. Quick reference guides for the languages are also included.

Uploaded by

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

Notes for Coding Test

Asokan Pichai
April 2021

1 Purpose of these notes


We are offering you solutions to the problems given in the practice tests. Often
multiple solutions using different language constructs are available. The idea is
to offer you samples of good coding style, example uses of such constructs, and
a starting point to learn the language in more detail.
Solutions are in C, C++, Java and Python.
We have also added quick reference guides for the languages at the end.

2 Coding Problems
2.1 To find the largest of three numbers
Write a function that returns the largest of its three integer arguments.

C Solution 1

int l a r g e s t ( int a , int b , int c ) {


i f ( a > b && a > c ) {
return a ;
}
i f (b > c ) {
return b ;
}
return c ;
}

1
C Solution 2

int l a r g e s t ( int a , int b , int c ) {


int answer = a ;
i f ( answer < b ) {
answer = b ;
}
i f ( answer < c ) {
answer = c ;
}
return answer ;
}

C Solution 3

int l a r g e r ( int a , int b ) {


return a > b? a : b ;
}

int l a r g e s t ( int a , int b , int c ) {


return l a r g e r ( a , l a r g e r ( b , c ) ) ;
}

Other languages
C++ The solutions are identical.
Java One needs to add public static to the declarations.
Python Just use the built-in max()

2.2 Digital root of a number


Write a function that computes the digital root of the integer given to it. The
digital root of a number is obtained by summing all the digits in a number,
repeatedly, till we get a single digit.

2
C Solution 1

int d i g i t s u m ( int n ) {
int ds = 0 ;
while ( n > 0 ) {
ds += n % 1 0 ;
n /= 1 0 ;
}
return ds ;
}

int d i g i t a l r o o t ( int a ) {
i f ( a <= 0 ) {
return 0 ;
}

int dr = a ;
do {
dr = d i g i t s u m ( dr ) ;
} while ( dr > 9 ) ;

return dr ;
}

C Solution 2

int d i g i t a l r o o t ( int a ) {
i f ( a <= 0 ) {
return 0 ;
}

int dr = 0 ;
while ( a > 0 ) {
dr += a % 1 0 ;
a /= 1 0 ;
}

while ( dr > 9 ) {
dr = d i g i t a l r o o t ( dr ) ;
}

return dr ;
}

3
C Solution 3

int d i g i t a l r o o t ( int a ) {
i f ( a <= 0 ) {
return 0 ;
}
a %= 9 ;
i f ( a == 0 ) {
return 9 ;
} else {
return a ;
}
}

C Solution 4

int d i g i t a l r o o t ( int a ) {
i f ( a <= 0 ) {
return 0 ;
}
return 1 + ( a = 1 ) % 9 ;
}

2.2.1 Python Solution 1

def d i g i t a l s u m ( a : int ) => int :


ds = 0
while a > 0 :
ds += a % 10
a //= 10
return ds

def d i g i t a l r o o t ( n : int ) => int :


i f n <= 0 :
return 0

n = digitalsum (n)
while n > 9 :
n = digitalsum (n)
return n

4
2.2.2 Python Solution 2

def d i g i t a l r o o t ( a : int ) => int :


i f a <= 0 :
return 0

ds = 0
while a > 0 :
ds += a % 10
a //= 10
while ds > 9 :
ds = d i g i t a l r o o t ( ds )
return ds

2.2.3 Python Solution 3

def d i g i t a l r o o t ( a : int ) => int :


i f a <= 0 :
return 0

a %= 9
return 9 i f a == 0 e l s e a

2.2.4 Python Solution 4

def d i g i t a l r o o t ( a : int ) => int :


i f a <= 0 :
return 0

return 1 + ( a = 1 ) % 9

2.3 Are all digits even?


Write a function that checks if all the digits of the given integer argument are
even. Here we will give the Java solution. You can make the minor changes
necessary to get the C and C++ versions.

5
2.3.1 Java Solution 1

class EvenDigits {
public s t a t i c boolean a r e A l l D i g i t s E v e n ( int n ) {
while ( n > 0 ) {
i f ( n % 2 == 1 ) {
return f a l s e ;
}
n /= 1 0 ;
}
return true ;
}

2.3.2 Python Solution 1

def a r e A l l D i g i t s E v e n ( n : int ) => bool :


while n > 0 :
i f ( n % 2 == 1 )
return F a l s e
n //= 10
return True

2.3.3 Python Solution 2

def a r e A l l D i g i t s E v e n ( n : int ) => bool :


return a l l ( [ ch in ” 02468 ” f o r ch in s t r ( n ) ] )

2.3.4 Python Solution 3

def a r e A l l D i g i t s E v e n ( n : int ) => bool :


return a l l ( [ int ( ch ) % 2 == 0 f o r ch in s t r ( n ) ] )

2.4 Armstrong Number


Determine if the number given is an Armstrong number.
An Armstrong number of n digits is a number that has the following prop-
erty: the sum of the nth powers of the digits of the number is equal to the
number itself. These are also called Narcissistic numbers.

6
2.4.1 Java Solution

c l a s s Armstrong {
public s t a t i c int power ( int a , int n ) {
int a p o w e r n = 1 ;
f o r ( int i = 0 ; i < n ; ++i ) {
a p o w e r n *= a ;
}
return a p o w e r n ;
}

public s t a t i c boolean i s A r m s t r o n g ( int a ) {


int n d i g i t s = I n t e g e r . t o S t r i n g ( a ) . l e n g t h ( ) ;
int sumOfPowersOfDigits = 0 ;
f o r ( int n = a ; n > 0 ; n/= 1 0 ) {
sumOfPowersOfDigits += power ( ( n%10) , n d i g i t s ) ;
}
return a == sumOfPowersOfDigits ;
}
Of course, we can use Math.power() and cast the result to an int, instead of
writing our own integer power function.

2.4.2 Python Solution

def n u m 2 d i g i t s ( n : int ) => [ int ] :


return [ int ( ch ) f o r ch in s t r ( n ) ]

def power ( ds : [ int ] ) => [ int ] :


return [ d ** len ( ds ) f o r d in ds ]

def i s a r m s t r o n g ( n : int ) => bool :


return n == sum( power ( n u m 2 d i g i t s ( n ) ) )

2.5 Kaprekar’s Constant


Take a four-digit number – that should not have all digits the same. Generate
the largest possible four-digit number, using the digits as well as the smallest
possible 4 digit number (padding the zeros in front if needed). Find the dif-
ference between the two. Repeat the process till the successive steps repeat.
Output the sequence of numbers, including the given number.

2.5.1 C++ Solution

7
#include <v e c t o r >

using namespace s t d ;

v e c t o r <int> n u m 2 d i g i t s ( int n ) {
v e c t o r <int> d i g i t s ( 4 , 0 ) ;
while ( n > 0 ) {
d i g i t s . i n s e r t ( d i g i t s . begin ( ) , n % 1 0 ) ;
n /= 1 0 ;
}
return d i g i t s ;
}

int d i g i t s 2 n u m ( v e c t o r <int> ds ) {
return 1000 * ds . a t ( 0 ) + 100 * ds . a t ( 1 ) +
10 * ds . a t ( 2 ) + ds . a t ( 3 ) ;
}

int n e x t k a p ( int n ) {
v e c t o r <int> d i g i t s = n u m 2 d i g i t s ( n ) ;

s o r t ( d i g i t s . b e g i n ( ) , d i g i t s . end ( ) ) ;
int mini = d i g i t s 2 n u m ( d i g i t s ) ;

r e v e r s e ( d i g i t s . b e g i n ( ) , d i g i t s . end ( ) ) ;
int maxi = d i g i t s 2 n u m ( d i g i t s ) ;

return maxi = mini ;


}

v e c t o r <int> k a p r e k a r s e q ( int n ) {
v e c t o r <int> k a p s e q ;
while ( true ) {
k a p s e q . push back ( n ) ;
int next = n e x t k a p ( n ) ;
i f ( n == next ) {
break ;
}
n = next ;
}
return k a p s e q ;
}

8
2.5.2 Python Solution

def n u m 2 d i g i t s ( n : int ) => [ int ] :


return [ int ( ch ) f o r ch in s t r ( n ) ]

def d i g i t s 2 n u m ( ds : [ int ] ) => int :


i f len ( ds ) == 1 :
return ds [ = 1]
else :
return 10 * d i g i t s 2 s u m ( ds [ : = 1 ] ) + ds [ = 1]

def n e x t k a p ( n : int ) => int :


d i g i t s = num2digits (n)
return d i g i t s 2 n u m ( sorted ( d i g i t s , r e v e r s e = True ) )
= d i g i t s 2 n u m ( sorted ( d i g i t s ) )

def k a p r e k a r s e q ( n : int ) => [ int ] :


next = n e x t k a p ( n )
i f n == next :
return [ n ]
else :
return [ n ] + k a p r e k a r s e q ( next )

2.6 Is a number perfect?


A number is said to be perfect if it is the sum of all its proper divisors. 6 =
1 + 2 + 3 and 28 = 1 + 2 + 4 + 7 + 14 are two well-known examples. If the
sum of the proper factors is greater than the number then the number is termed
abundant; if the sum of the factors is less than the number it is termed deficient.
Write a function that checks if a number is perfect.

2.6.1 C Solution

int s u m o f f a c t o r s ( int n ) {
int s o f = 1 ;
f o r ( int f a c t o r = 2 ; f a c t o r <= n / 2 ; ++f a c t o r ) {
i f ( n % f a c t o r == 0 ) {
s o f += f a c t o r ;
}
}
return s o f ;
}

b o o l i s p e r f e c t ( int n ) {
return n == s u m o f f a c t o r s ( n ) ;

9
}
Java, C++ solutions are close to this.

2.6.2 Python Solution

def s u m o f f a c t o r s ( n : int ) => int :


return sum ( [ f f o r f in range ( 1 , 1 + n / / 2 ) i f n % f == 0 ] )

def i s p e r f e c t ( n : int ) :
return n == s u m o f f a c t o r s ( n ) ;

2.7 Super palindrome


A string is termed to be a palindrome if it reads the same way, forward and
backward: ”MADAM” ”MALAYALAM”, ”TAPPET” are some examples.
A super palindrome relaxes two conditions: the comparison of the reversed
string being the same 1. ignores case 2. ignores spaces. Thus ”Madam Im
Adam” is a super palindrome.
Write a function that checks if a string is a super-palindrome.

2.7.1 C Solution 1

void make super ( char * s ) {


char b u f f e r [ 1 0 2 4 ] ;
int bpos = 0 ;

f o r ( int pos = 0 ; s [ pos ] != ’ \0 ’ ; ++pos ) {


i f ( s [ pos ] != ’ ’ ) {
b u f f e r [ bpos++] = t o l o w e r ( s [ pos ] ) ;
}
}
b u f f e r [ bpos ] = ’ \0 ’ ;
strcpy ( s , buffer ) ;
return ;
}

b o o l i s p a l i n d r o m e ( char * s ) {
int i = 0 ;
int j = s t r l e n ( s ) = 1 ;

while ( i < j ) {
i f ( s [ i ] != s [ j ] ) {
return f a l s e ;
}

10
++i ;
== j ;
}
return t r u e ;
}

b o o l i s s u p e r p a l i n d r o m e ( char * s ) {
make super ( s ) ;
return i s p a l i n d r o m e ( s ) ;
}

2.7.2 C Solution 2

b o o l i s s u p e r p a l i n d r o m e ( char * s ) {
int i = 0 ;
int j = s t r l e n ( s ) = 1 ;

while ( i < j ) {
while ( s [ i ] == ’ ’ ) {
++i ;
}
while ( s [ j ] == ’ ’ ) {
== j ;
}
i f ( s [ i ] != s [ j ] ) {
return f a l s e ;
}
++i ;
== j ;
}
return t r u e ;
}
The first approach is ok for c++; and can be easily modified to work for
String s, instead of char* s.
Similarly the code is easy to change for Java.

2.7.3 Python Solution

def make super ( s : s t r ) => s t r :


return ’ ’ . j o i n ( [ ch f o r ch i s s i f ch != ’ ’ ] ) . l o w e r ( )

def i s p a l i n d r o m e ( s : s t r ) => bool :


return s == s [ : : = 1 ]

11
def i s s u p e r p a l i n d r o m e ( s : s t r ) => bool :
s = make super ( s )
return i s p a l i n d r o m e ( s )

2.8 Sum of even evens


Write a function that computes the sum of all even numbers that occur in even
indices in a given array.

2.8.1 Python Solution 1

def evenevensum ( a : [ int ] ) => int :


return sum ( [ b f o r b in a [ : : 2 ] i f b % 2 == 0 ]

2.8.2 Python Solution 2

def evenevensum ( a : [ int ] ) => int :


return sum ( [ b i f b % 2 == 0 e l s e 0 f o r b in a ] [ : : 2 ] )

2.8.3 Python Solution 3

def even ( n : int ) => bool :


return n % 2 == 0

def evenevensum ( a : [ int ] ) => int :


return sum( f i l t e r ( even , a [ : : 2 ] ) )

12

You might also like