Tuesday, July 1, 2014

Some very common interview questions for programmers of the old days

Some companies these days have invested in third parties, which assess candidates effectively. But before that, let's visit the old days.

1) Palindrome, a very famous common question... (why is it very common?)

 // no encoding (rosettacode.org)  
 func reverseBytes(s string) string {  
   r := make([]byte, len(s))  
   for i := 0; i < len(s); i++ {  
     r[i] = s[len(s)-1-i]  
   }  
   return string(r)  
 }  
 func isPalindrome(s string) bool {  
   return s == reverseBytes(s)  
 }  
 func main() {  
   fmt.Println( isPalindrome("addrian") );  
   fmt.Println( isPalindrome("aba") );  
 }  

2) A sequence of numbers from 1 to N is arranged in random order.
   A number is removed from the set, what is that number?

 func getSumOfAllN(arr []int) int {  
   sum := 0  
   for _,n := range arr {  
     sum += n  
   }  
   return sum    
 }  
 func getArithSequence(count int) int {  
   return ( (count*(count+1)) / 2 )  
 }  
 func getMissingN(arr []int) int {  
   return getArithSequence( len(arr)+1 ) - getSumOfAllN(arr)  
 }  
 func main() {  
   // setup input  
   arr := []int{}  
   const missing int = 18  
   for i:=1; i<=100; i++ {  
     if missing == i {  
       continue  
     }  
     arr = append(arr, i)  
   }  
   // execute test  
   fmt.Println( "the missing number is: ", getMissingN(arr) )    
 }  

3) Print whether the inputted integer is positive or negative without using a conditional statement

 var (  
   g_msgs = []string{"+", "-"}  
 )  
 func prn(s string) bool {  
   fmt.Println(s)  
   return true  
 }  
 func prnPosNeg(n int32) {  
   _ = (0==n) && prn("either")  
   _ = (0!=n) && prn( g_msgs[(n>>31)&0x01] )  
 }  
 func main() {  
   prnPosNeg(0)  
   prnPosNeg(1)  
   prnPosNeg(2)  
   prnPosNeg(-1)  
   prnPosNeg(-2)  
 }