# Cracking the coding interview | Step-6

### DAY 6: coding interview questions

Question 1:

Is Palindrome Number?

Write a function to find whether the given number N is a palindrome.
A palindrome number is one that reads the same backwards as well as forwards. For e.g. 252, 18981, 5005 are examples of palindrome numbers.
The number will be passed to the function as an input parameter of type int.
If the number is a palindrome, the function should return 2, else it should return 1.
Assumption: The input number will be a positive integer number >= 1 and <= 25000

//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
public int isPalinNum(int input1){
String str=Integer.toString(input1);
int len=str.length();
String str1="";
for(int i=len-1;i>=0;i--)
{
str1+=str.charAt(i);
}
if(str.equals(str1))
return 2;
else
return 1;
}
}

Question 2:

Is Palindrome possible?

Write a function to find whether it is possible to get a palindrome number from a given number by re-arranging the positions of its digits. If yes, the function should return 2, else it must return 1.
Example1: If the given number is 21251, it is possible to form a palindrome by re-arranging its digits, as 21512 or 12521. So the function must return 2.
Example2: If the given number is 2125, it is not possible to form a palindrome by re-arranging its digits. So the function must return 1.
Note: All diits of the given number should be retained while deciding whether they can together form a palindrome.
Assumption: The input number will be a positive integer number >= 1 and <= 25000.

//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
public int isPalinNumPossible(int input1){
String str=Integer.toString(input1);
int count[] = new int[256];
Arrays.fill(count, 0); // to initialize all values to zero
for (int i = 0; i < str.length(); i++)
count[(int)(str.charAt(i))]++;
int odd = 0;
for (int i = 0; i < 256; i++)
{
if ((count[i] & 1) == 1)
odd++;
if (odd > 1)
return 1;
}
return 2;
}
}

Question 3:

Create PIN using three given input numbers

"Secure Assets Private Ltd", a small company that deals with lockers has recently started manufacturing digital locks which can be locked and unlocked using PINs (passwords). You have been asked to work on the module that is expected to generate PINs using three input numbers.
Assumptions: The three given input numbers will always consist of three digits each i.e. each of them will be in the range >=100 and <=999
100 <= input1 <= 999
100 <= input2 <= 999
100 <= input3 <= 999
Below are the rules for generating the PIN -
- The PIN should be made up of 4 digits
- The unit (ones) position of the PIN should be the least of the units position of the three input numbers
- The tens position of the PIN should be the least of the tens position of the three input numbers
- The hundreds position of the PIN should be the least of the hundreds position of the three input numbers
- The thousands position of the PIN should be the maximum of all the digits in the three input numbers
Example 1 -
input1 = 123
input2 = 582
input3 = 175
then, PIN = 8122
Example 2 -
input1 = 190
input2 = 267
input3 = 853
then, PIN = 9150
*/
//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
public int createPIN(int input1,int input2, int input3){
int arr[]={input1,input2,input3};
int max=0,min;
double sum=0.0;
double place=1.0;
int num;
for(int i=0;i<3;i++)
{
num=arr[i];
while(num!=0)
{
int r=num%10;
if(r>max)
max=r;
num=num/10;
}
}
for(int i=0;i<3;i++)
{
min=99;
for(int j=0;j<3;j++)
{
int rem=arr[j]%10;
if(rem<min)
min=rem;
arr[j]/=10;
}
sum=(min+(sum/place));
sum*=place;
place*=10;
}
return (int)(max*1000+sum);
}
}
}
}

Question 4:

Weight of a hill pattern

Given,
the total levels in a hill pattern (input1),
the weight of the head level (input2), and
the weight increments of each subsequent level (input3),
you are expected to find the total weight of the hill pattern.
"Total levels" represents the number of rows in the pattern.
"Head level" represents the first row.
Weight of a level represents the value of each star (asterisk) in that row.
The hill patterns will always be of the below format, starting with 1 star at head level and increasing 1 star at each level till level N.

*
**
***
****
*****
******
. . .and so on till level N
Let us see a couple of examples.
Example1 -
Given,
the total levels in the hill pattern = 5 (i.e. with 5 rows)
the weight of the head level (first row) = 10
the weight increments of each subsequent level = 2
Then, The total weight of the hill pattern will be calculated as = 10 + (12+12) + (14+14+14) + (16+16+16+16) + (18+18+18+18+18) = 10 + 24 + 42 + 64 + 90 = 230
Example2 -
Given,
the total levels in the hill pattern = 4
the weight of the head level = 1
the weight increments of each subsequent level = 5
Then, Total weight of the hill pattern will be = 1 + (6+6) + (11+11+11) + (16+16+16+16) = 1 + 12 + 33 + 64 = 110

//Code:
import java.io.*;
import  java.util.*;
class UserMainCode
{
public int totalHillWeight(int input1, int input2, int input3){
int sum=0;
for(int i=0;i<input1;i++)
{
for(int j=0;j<=i;j++)
{
sum+=input2;
}
input2+=input3;
}
return sum;
}
}