What is polymorphism?

The beauty of polymorphism is that the code working with the different classes does not need to know which class it is using since they’re all used the same way.

A real world analogy for polymorphism is a button. Everyone knows how to use a button: you simply apply pressure to it. What a button “does,” however, depends on what it is connected to and the context in which it is used — but the result does not affect how it is used. If your boss tells you to press a button, you already have all the information needed to perform the task.

In the programming world, polymorphism is used to make applications more modular and extensible. Instead of messy conditional statements describing different courses of action, you create interchangeable objects that you select based on your needs. That is the basic goal of polymorphism.

 

Source here.

What demonstrates good code quality?

Efficient, reliable, high availability (services), secure, maintainable, reusable, extensible.

Good code is easy to understand, use, and maintain. Variables, functions, classes, etc, should be named in an appropriate manner, enabling the next person after you to effectively reuse or modify them.

It should be well tested. The code should have high test coverage – this will help you write code that works thoroughly, and help avoid any regressions in code functionality once more work is done on it in the future.

The code should demonstrate low coupling and high cohesion. From stackoverflow:

Cohesion refers to what the class (or module) will do. Low cohesion would mean that the class does a great variety of actions and is not focused on what it should do. High cohesion would then mean that the class is focused on what it should be doing, i.e. only methods relating to the intention of the class.

Example of Low Cohesion:

-------------------
| Staff           |
-------------------
| checkEmail()    |
| sendEmail()     |
| emailValidate() |
| PrintLetter()   |
-------------------

Example of High Cohesion:

----------------------------
| Staff                   |
----------------------------
| -salary                 |
| -emailAddr              |
----------------------------
| setSalary(newSalary)    |
| getSalary()             |
| setEmailAddr(newEmail)  |
| getEmailAddr()          |
----------------------------

As for coupling, it refers to how related are two classes / modules and how dependent they are on each other. Being low coupling would mean that changing something major in one class should not affect the other. High coupling would make your code difficult to make changes as well as to maintain it, as classes are coupled closely together, making a change could mean an entire system revamp.

All good software design will go for high cohesion and low coupling.

 

Source here.

What is a singleton pattern?

Singleton pattern solves the problem of having a one and only one instance of a class in your project. Singleton is nothing more than a fancy name for a global variable.

 

<?php
class Singleton
{
    /**
     * @var Singleton The reference to *Singleton* instance of this class
     */
    private static $instance;
    
    /**
     * Returns the *Singleton* instance of this class.
     *
     * @return Singleton The *Singleton* instance.
     */
    public static function getInstance()
    {
        if (null === static::$instance) {
            static::$instance = new static();
        }
        
        return static::$instance;
    }

    /**
     * Protected constructor to prevent creating a new instance of the
     * *Singleton* via the `new` operator from outside of this class.
     */
    protected function __construct()
    {
    }

    /**
     * Private clone method to prevent cloning of the instance of the
     * *Singleton* instance.
     *
     * @return void
     */
    private function __clone()
    {
    }

    /**
     * Private unserialize method to prevent unserializing of the *Singleton*
     * instance.
     *
     * @return void
     */
    private function __wakeup()
    {
    }
}

class SingletonChild extends Singleton
{
}

$obj = Singleton::getInstance();
var_dump($obj === Singleton::getInstance());             // bool(true)

$anotherObj = SingletonChild::getInstance();
var_dump($anotherObj === Singleton::getInstance());      // bool(false)

var_dump($anotherObj === SingletonChild::getInstance()); // bool(true)

What is the point of using interfaces?

The point of using an interface has to do with code organization for me, than enforcement by the language itself. I use them a lot when preparing code for other developers to work with so that they stay within the intended design patterns. Interfaces are a kind of “design by contract” whereby your code is agreeing to respond to a prescribed set of API calls that may be coming from code you do not have access to.

Definition

  • All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.
  • Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled
  • Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
  • All methods declared in an interface must be public; this is the nature of an interface.
  • Interfaces allow you to create code which defines the methods of classes that implement it. You cannot however add any code to those methods.
  • To implement an interface, the implements operator is used.
  • It’s possible for interfaces to have constants. Interface constants works exactly like class constants except they cannot be overridden by a class/interface that inherits them.

Source: php.net

 

Yelp phone screening interview

During my interview with Yelp today I was asked the following questions:

  1. Why would you like to work for Yelp?
  2. How many bits can be represented by a single octal digit? Answer: 3 Source
  3. What should be the sizeof(int) on a 64 bit machine?
  4. How many bits can be represented by a single octal digit?
  5. On which data structure it would be faster to perform binary search linked list or sorted array?
  6. What is the worst case time of inserting element in the beginning linked list?
  7. What is the number for the kill signal in unix?
  8. How does a domain name map to a server on the internet?
  9. What does ssl stand for?
  10. Something about finding all references of a word or function under linux. I said use grep command and it seemed to be right.
  11. Something about mutable in Python. I don’t know Python so I didn’t answer it.

What happens when you type in a URL in browser?

In a simplified way:

  1. browser checks cache; if requested object is in cache and is fresh, skip to #9
  2. browser asks OS for server’s IP address
  3. OS makes a DNS lookup and replies the IP address to the browser
  4. browser opens a TCP connection to server (this step is much more complex with HTTPS)
  5. browser sends the HTTP request through TCP connection
  6. browser receives HTTP response and may close the TCP connection, or reuse it for another request
  7. browser checks if the response is a redirect (3xx result status codes), authorization request (401), error (4xx and 5xx), etc.; these are handled differently from normal responses (2xx)
  8. if cacheable, response is stored in cache
  9. browser decodes response (e.g. if it’s gzipped)
  10. browser determines what to do with response (e.g. is it a HTML page, is it an image, is it a sound clip?)
  11. browser renders response, or offers a download dialog for unrecognized types

You can  get more information here.

What does TCP/IP stand for?

TCP/IP (Transmission Control Protocol/Internet Protocol) is the basic communication language or protocol of the Internet. It can also be used as a communications protocol in a private network (either an intranet or an extranet). When you are set up with direct access to the Internet, your computer is provided with a copy of the TCP/IP program just as every other computer that you may send messages to or get information from also has a copy of TCP/IP.