Bloomberg LP Interview Question
InternsCountry: United States
Interview Type: Phone Interview
// convert to a string then compare
bool palindrome(int a){
int size = 0, temp = a;
while( temp /= 10) size++;
char str[size + 1];
sprintf(str, "%d", a);
char *start = str;
char *end = str + size - 1;
while(end > start){
if(*start != *end) return false;
start ++;
end --;
}
return true;
}
I have three methods, first, if we can use extra space we can use sprintf to convert int to string to judge whether this int is a palindrome. Second, we use / operator to reverse the int and judge whether the reversed number and the raw number are equal. Third, I will give you my program here. A very important point is that the second method maybe cause overflow.
#include<stdio.h>
#include<string.h>
int auxiliary(char *p_start, char *p_end) {
while(p_start < p_end) {
if(*p_start != *p_end) {
break;
}
p_start++;
p_end--;
}
if(p_start >= p_end) {
return 1;
} else {
return 0;
}
}
int is_palindrome1(int n) {
char s[10] = {'\0'};
sprintf(s, "%d", n);
int len = strlen(s);
char *p_start = s;
char *p_end = s + len - 1;
if(auxiliary(p_start, p_end)) {
return 1;
} else {
return 0;
}
}
int is_palindrome2(int n) {
int value = 0;
int temp = n;
while(temp) {
value = value * 10 + temp % 10;
temp /= 10;
}
if(value == n) {
return 1;
} else {
return 0;
}
}
int is_palindrome3(int n) {
int div = 1;
int temp = n;
int first, last;
while(temp >= 10) {
div *= 10;
temp /= 10;
}
while(n) {
first = n / div;
last = n % 10;
if(first != last) {
break;
}
n = (n % div) / 10;
div /= 100;
}
if(n == 0) {
return 1;
} else {
return 0;
}
}
void main() {
int n = 12344321;
if(is_palindrome3(n)) {
printf("yes\n");
} else {
printf("no\n");
}
getchar();
}
public static void main(String[] args) {
String s = new Integer(12521).toString();
StringPalindrome stringPalindrome = new StringPalindrome();
System.out.println(stringPalindrome.checkPalindrome(s,0,s.length()-1));
}
private boolean checkPalindrome(String s,int start, int end ){
while(start <= end){
if(s.charAt(start) == s.charAt(end)){
start++;end--;
}else return false;
}
return true;
}
public class palindrome {
private static Scanner input;
public static void main(String[] args) {
input = new Scanner(System.in);
String userIp = input.next();
int len = userIp.length();
int mid = len/2;
int i=0;int left,right;
boolean flag= true;
for(i=0;i<mid;i++){
left = mid+i;
right = mid-i;
if(userIp.charAt(left) != userIp.charAt(right)){
System.out.println("Not Palindrome");
flag = false;
}
}
if(flag)
System.out.println("Palindrome");
}
}
bool isPalindrome(int number)
{
int d[50],i =0;
while(int digit = number %10, number >>1 !=0)
{
d[i++]=digit;
}
for(int j=0;j<i/2;j++)
if(d[j]!=d[i-j])
return false;
}
return true;
Simple trick would be to reverse a number and check if it is equal to original number.
- ashishB February 03, 2013If yes, number is a palindrome.
main()
{
int temp1, temp2;
int n=0;
printf("Number plz");
scanf("%d",&temp1);
temp2=temp1;
while(temp2!=0)
{
n=n*10;
n=n+temp2%10;
temp2=temp2/10;
}
if(temp1==n)
printf("palindrom found");
else
printf("no palindrome");
}