Thursday, May 26, 2011

The classic Palindrome problem

#include<stdio.h>
#include<ctype.h>
#include<string.h>

int isPalindrome( char *s );

int main ( void )
{
   int i = 0;
   int ch;
   char s[100];
 
   while ((ch = getchar()) != ('\n')) {
      if (isalpha(ch)) {
         ch = toupper(ch);
         s[i] = ch;
         i++;
      }
   }
   s[i]='\0';
 
   if ( isPalindrome(s) == 1) {
      printf("Yes, it is a palindrome.\n");
   } else {
      printf("No, it is not a palindrome.\n");
   }
 
   getchar();  
   return 0;
 
}
 
int isPalindrome( char *s )
{
   int i = strlen(s)-1;
   int j = 0;
 
   while (j < i) {
      if(s[j] != s[i]) {
         return 0;
      }
      i--;
      j++;
   }
   return 1;
}

A good video to review OOP concepts


From www.youtube.com

The classic "atoi" question

//Language C++
#include <iostream>
using namespace std;

int atoi(char *s)
{
  int i=0;
  while(*s)
  {
    if (*s ==' ' || *s =='\t')
       s++;
    else {
    if ( *s < '0' || *s > '9' )
         return i;  
    i = i*10 + (*s- '0');
    s++;
    }
  }
  return i;
}

int my_atoi(char* pStr) {

    if (pStr == NULL) {
        printf("ERROR: null string.\n");
        return -1;
    }

    int num = 0;

    int pos;
    for (pos = 0; ; pos++) {
        char currChar = *(pStr+pos);

        // Check whether the current char is not a digit
        if ( currChar < '0' || currChar > '9' )
            return num;

        // Read the number and add it to the 'num' variable
        num = (num*10) + (int) currChar - (int)'0';
    }
    return num;
}

int s_atoi(char *s)
{
  int i = 0;
  while(*s)
  {
    if ( *s < '0' || *s > '9' )
         return i;
    i = (i<<3) + (i<<1) + (*s - '0');
    cout <<i <<endl;
    s++;
  }
  return i;
}

/////////////
int Atoi(const char *pstr)
{
int sign = 1;
int num = 0;
while (*pstr == ' ' || *pstr == '\t')
{
pstr++;
}
if (*pstr == '-')
{
sign = -1;
pstr++;
}
while (*pstr)
{
if (*pstr >= '0' && *pstr <= '9')
{
num = 10 * num + *pstr - '0';
}
else
{
return num * sign;
}
pstr++;
}
return (num * sign);
}
//////////////

int main(){
char *s = "  9885 pigs";
cout << atoi(s) <<endl;
getchar();
cout << my_atoi(s) <<endl;
getchar();
cout << s_atoi(s) <<endl;
getchar();
cout << Atoi(s) <<endl;
getchar();
cout<<"-----ascii----"<<endl;
for(char ch = 'A'; ch <= 'Z'; ch++)
//insert cout statement here, as clearly your professor wont like
    printf("%x %i %c \n" , ch,ch,ch);
    getchar();
return 0;
}

Warm up question: What is the difference between Hashtable and Hashmap?

Notes on Hashtable and Hashmap.
      Both provide key-value access to data.
  • HashMap permits null value while Hashtable does not.
  • Access to Hashtable is synchronized on the table while access to the HashMap is not (by default).
  • Iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not.
  • Hashtable is an original collection class in Java while HashMap is part of the new collections framework.



Tuesday, May 24, 2011

Welcome to Code Interview!

This is the platform to prepare for your technical interviews. Welcome!

I will keep on posting the recent code interview problems from leading IT and finance companies (i.e., Amazon.com, Google, Microsoft, Facebook, Bloomberg, etc.) as well as the answers. The blog is designed to help you, the feature IT leaders, to prepare for the code interview with world leading companies.

Comments and suggestions are welcome!