Notes For Coding Test: 1 Purpose of These Notes
Notes For Coding Test: 1 Purpose of These Notes
Asokan Pichai
April 2021
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
1
C Solution 2
C Solution 3
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
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 ;
}
n = digitalsum (n)
while n > 9 :
n = digitalsum (n)
return n
4
2.2.2 Python Solution 2
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
a %= 9
return 9 i f a == 0 e l s e a
return 1 + ( a = 1 ) % 9
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 ;
}
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 ;
}
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 ) ;
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
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.
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.1 C Solution 1
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.
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 )
12