Technical Interview Question – Pascal’s Triangle

Here is more complicated question which is sometimes asked during a technical interview.

Write a function that prints out the first n rows of Pascal’s triangle (with f(1) yielding the row consisting of only the element 1). This can be done either by summing elements from the previous rows or using a binary coefficient or combination function. Behavior for n <= 0 does not need to be uniform, but should be noted.

Pascal’s triangle is an arithmetic and geometric figure first imagined by Blaise Pascal. Its first few rows look like this:

   1
  1 1
 1 2 1
1 3 3 1

Each element of each row is either 1 or the sum of the two elements right above it. For example, the next row would be 1 (since the first element of each row doesn't have two elements above it), 4 (1 + 3), 6 (3 + 3), 4 (3 + 1), and 1 (since the last element of each row doesn't have two elements above it). Each row n (starting with row 0 at the top) shows the coefficients of the binomial expansion of (x + y)n.

function pascalsTriangle($num)
{
    $c = 1;
    $triangle = Array();

    for($i=0;$i<=$num;$i++) {
        $triangle[$i] = Array();
        if(!isset($triangle[$i-1])) {
            $triangle[$i][] = $c;
        }
        else{
            for($j=0; $j < count($triangle[$i-1])+1; $j++){
                if (isset($triangle[$i-1][$j-1]) && isset($triangle[$i-1][$j])) {
                    $triangle[$i][] = $triangle[$i-1][$j-1] + $triangle[$i-1][$j] 
                }
                else {
                    $triangle[$i][] = $c;
                }
            }
        }
    }

    return $triangle;
}
 
$tria = pascalsTriangle(8);
foreach($tria as $val) {
    foreach($val as $value) {
        echo $value . ' ';
    }
    echo '
'; }
Posted in Interview Questions, PHP | Leave a comment

Technical Interview Question – Fizz Buzz

Here is a common question that’s usually asked in the first-round interview stage. The typical question is something like:

Print out the numbers 1 to 100. Where the number is a multiple of 3, print ‘Fizz’, otherwise if it is a multiple of 5 print ‘Buzz’. If the number is a multiple of 3 and 5, print ‘FizzBuzz’.

// PHP

for ($i = 1; $i <= 100; $i++)
{
    if ($i % 3 == 0 && $i % 5 == 0) {
        echo 'FizzBuzz';
    }
    else if ($i % 3 == 0) {
        echo 'Fizz';
    }
    else if ($i % 5 == 0){
        echo 'Buzz';
    }
    else {
        echo $i;
    }
}
# PERL

for (my $count = 1; $count <= 100; $count++)
{
    if (($count % 3 == 0) && ($count % 5 == 0)) {
        print "FizzBuzz\n";
    }
    elsif ($count % 3 == 0) {
        print "Fizz\n";
    }
    elsif ($count % 5 == 0) {
        print "Buzz\n";
    }
    else {
        print $count . "\n";
    }
}
// JAVASCRIPT

for (var count = 1; count <= 100; count++)
{
    if ((count % 3 == 0) && (count % 5 == 0)) {
        console.log('FizzBuzz');
    }
    else if (count % 3 == 0) {
        console.log('Fizz');
    }
    else if (count % 5 == 0) {
        console.log('Buzz');
    }
    else {
        console.log(count);
    }
}
// JAVA

public class FizzBuzz {                      
    public static void main(String[] args) {   
        for(int i = 1; i <= 100; i++) {                    
            if (((i % 5) == 0) && ((i % 3) == 0))            
                System.out.println("FizzBuzz");    
            else if (i % 3 == 0) {
                System.out.println("Fizz"); 
            }
            else if (i % 5 == 0) {
                System.out.println("Buzz"); 
            }
            else {
                System.out.println(i);
            }
        }
    }
}
// PYTHON

for count in xrange(1,101):
    if count % 5 == 0 and count % 3 == 0:
        print "FizzBuzz"
    elif count % 3 == 0:
        print "Fizz"
    elif count % 5 == 0:
        print "Buzz"
    else:
        print count
Posted in Interview Questions, Java, Javascript, Perl, PHP | Tagged , , , , | Leave a comment

Abstract versus Interface

An Abstract class without any implementation just looks like an Interface; however there are more differences than similarities between the two.

Abstract Class

An abstract class is a special kind of class that cannot be instantiated and is only to be inherited from. The advantage is that it enforces certain hierarchies for all the subclasses. It is a contract that forces all the subclasses to carry on the same hierarchies or standards.

Interface

An interface is not a class. It is an entity that is defined by the word Interface. An interface has no implementation; it only has the signature or in other words, just the definition of the methods without the body. One of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between is that a class can implement more than one interface but can only inherit from one abstract class.

Together

With an interface, we are basically creating a set of methods without any implementation that must be overridden by the implemented classes. The advantage is that it provides a way for a class to be a part of two classes: one from inheritance hierarchy and one from the interface.

When we create an abstract class, we are creating a base class that might have one or more completed methods but at least one or more methods are left uncompleted and declared abstract. If all the methods of an abstract class are uncompleted then it is same as an interface. The purpose of an abstract class is to provide a base class definition for how a set of derived classes will work and then allow the programmers to fill the implementation in the derived classes.

Posted in Java, PHP, Software Engineering, Uncategorized | Leave a comment