Recursion

Data structures and algorithm play an important role in any programming job interview. It is important because it consists of basic programming concept which every programmer should know by default like stack, queue, linked list, array, tree and graph. Actually algorithm are way to solve problems using above mentioned data structures.  There may be many ways or algorithms to solve the problem. Recursion is method where the solution to a problem depends on solutions to smaller instances of the same problem. Generally people find 2-3 line recursive solution and memorise it but they don’t understand what happening inside the recursive call how scope and values of the variable changing inside the small piece of code.

So let’s understand what magic happens inside the tiny piece of recursive code. Recursion is basically dividing the main problem into sub-problem of the same type as original, solve the sub-problems and combine the results.

  • Defintion :  A functions that calls itself.
  • Basic Conditions :  1. The base case
    2. Recursive call
  • Types : 1. head Recursion.
  •             2. Tail Recursion.
  • Example :
    def fun(number)
      if (number > 0)
        fun(--number)
        puts number
        fun(--number)
      end
    end
  • Output: 0 1 2 0 3 0 1

Explanation :  Recursion Tree

                   fun(4)
                   /
                fun(3), puts(3), fun(2)(puts 0 1)
               /
           fun(2), puts(2), fun(1)(puts 0)
           /
       fun(1), puts(1), fun(0)(does nothing)
       /
    fun(0), puts(0), fun(-1) (does nothing)

Explanation of head and tails Recursion :

Head Recursion : functions call itself before the other operation performed inside the function.

Tail Recursion : function call itself after the other operation performed inside the function.

Example :

def tail(number)                def head(number)                
  if(number == 1)                 if(number == 0)
    return number                   return number  
  else                            else
    puts number                     head(number-1)
  end                             end 
  tail(number-1);                 puts number     
end                             end

SOLID – 5

“D” – Dependency inversion principle is the 5th SOLID concept after Interface Segregation principle. It states that high-level modules/classes should not depend upon low-level modules/classes. Both should depend upon abstractions. Secondly, abstractions should not depend upon details. Details should depend upon abstractions.

SOLID – 4

“I” – ISP (Interface Segregation principle)  is the 4th SOLID concept after Liskov substitution principle. It states that clients should not be forced to implement interfaces they don’t use.

interfacesegregationprinciple

for example class, each interface has its own responsibilities or purpose(single responsibility principle). If the interface has more number of methods, classes that implementing this will not implementing all the methods.

Let’s take an example to make it more clear :

class InterfaceLead 
  def assign_task
  end
  def create_sub_task
  end
  def work_on_task
  end 
end 

class TeamLead < InterfaceLead
  def assign_task
    #assigning task to do.
  end
  def create_sub_task
    #creating sub task from major tasks.
  end
  def work_on_task
    #work on task in order to complete it.
  end
end

In the above code there is one problem : TeamLead is not suppose to work on task, responsibilities that are assign to the InterfaceLead is forcing the Lead class to implement it i.e violating the principle of ISP.

So if we separate out the method: work_on_task from InterfaceLead and put it into another interface called InterfaceWork.

SOLID – 3

“L”- LSP (Liskov substitution principle) is the 3rd SOLID concept after Open closed principle. It states that “objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program(runtime polymorphism).”

liskovsubtitutionprinciple

Let’s understand this concept with the help of following ruby code :

class InterfaceDuck
  def swim 
  end
  #contract says that swimming? should be true if swim has been called.
  def swimming? 
  end
end
class RealDuck < InterfaceDuck
  def swim
    #do something to swim
  end
  def swimming? 
    #return true when duck is swimming.
  end
end
class ElectricDuck < InterfaceDuck
  def swim
    if (!battery_on?)
      return
    end
    #swim logic.
  end
end

and the calling code will be :

def make_duck_swim(InterfaceDuck duck) 
  duck.swim
end

as you can see above there are two example of duck interface, one is real duck  and other is electric duck.

The electric duck only swim if its battery is on. And if its battery is off it violate the the Liskov substitution principle, So to make it correct we must have to ON the battery of the electric duck.

def make_duck_swim(InterfaceDuck duck) 
  if (duck is electricDuck)
    battery_on == true
  duck.swim
end

Now, the above calling code will first check weather the duck is electric or real. If electric it make its battery_on == true else direct call the swim method as exactly defined in the interface.

SOLID – 2

“O” – Open closed principle – is a 2nd SOLID concept after SRP (Single responsibility principle). It states that “software entities… should be open for extension, but closed for modification.”

Let me explain this concept with the example :

class Customer
 #getter
 #setter
 def discount
 end
end
class SilverCustomer < Customer
 def discount
 50% OFF
 end
end
class PlatinumCustomer < SilverCustomer
 def discount
 75% OFF
 end
end

So, the above Customer class is open for extension as we have extended with SilverCustomer class and futher to PlatinumCustomer class or any other customer class can join the project. By default the behavior of the class should be private.

images

extensibility is as good as extending the wagons to the train.

SOLID Programming Principles

In computer programming, SOLID(Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion) are the basic principles of object oriented programming and designs. These SOLID principle concept are applied by programmer on software’s source code to remove the code smells by refactoring until it becomes maintainable and extensible over a time. These principles are the part of agile and adaptive programming.
These principle really takes time to design the architecture and to implement. Sometimes when you try to implement anyone of these, the other-one get violated. So the best way to hands-on is initiate with paper and pencil.

“S” – SRP (Single responsibility principle)- a class should have only a single responsibility (i.e. only one potential change in the software’s specification should be able to affect the specification of the class)

“O” – Open closed principle-“software entities … should be open for extension, but closed for modification.”

“L”- LSP (Liskov substitution principle)-“objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.” See also design by contract.

“I” – ISP (Interface Segregation principle)-“many client-specific interfaces are better than one general-purpose interface.”

“D” – Dependency inversion principle.- one should “Depend upon Abstractions. Do not depend upon concretions.”

Single responsibility principle :
Let’s get the idea what code is doing below

    class Person
      def get_name
        name
      end
      def get_telephone_number
          "(" + _office_area_code + ") " + office_number
      end
      def get_office_area_code
          office_area_code
      end

      def set_office_area_code(arg) 
        office_area_code = arg 
      end

      def get_office_number 
        office_number 
      end
    end

above Person class have lot of task to do. Person class is doing things which is not supposed to do. It should only validate the data and called to the other layer to perform their tasks.

28e3793fac99efb2637e5435cc35fc71

“It is not the load but OVERLOAD that kills” famous Spanish proverb. So the single responsibility principle says that  separate model for each entity.

How Do Browsers Work?

Its a very important question from the interview point of view, this question was asked to me in almost every technical interview that I faced in my college life. When you show your interest in networking, it will be the first question that is given to you. So don’t get scared by these kind of questions, the moment you type something in the address bar -for example “HTTP://WWW.GOOGLE.COM”- the following takes place.

Screen Shot 2015-07-22 at 3.56.00 pm

  • Resolve DNS
  • Request Page
  • Tokenize the response
  • Parse HTML
  • Build DOM tree
  • Build Render tree
  • Layout the Render tree
  • Painting

Let’s discuss about the above mentioned points with respect to OSI(Open System Interconnection) Model.

OSI-Communica

The application layer generates the packet to send on the network. This application layer also used for application authentication. Application layer has several protocols to help with information interchange. Some of the protocols are DNS(Domain Name System), HTTP(Hypertext Transfer Protocol), SOAP (Simple Object Access Protocol), IMAP (Internet Message Access Protocol), POP3 (Post Office Protocol Version 3), SMTP (Simple Mail Transfer Protocol), SIP (Session Initiation Protocol).

After the application layer when the request goes to the presentation layer, it get generified i.e HTML, XML. After that request goes to the session layer, session layer establish the  the session between source and destination. After session request goes to transport layer.

Transport Layer plays important role it provide end to end connection and used UDP and TCP protocol, and also add port address to the data packet, port address is basically door to a computer.

Network Layer adds the source and destination IP address which is obtained by DNS server in application layer.

After network layer, packet goes to data link layer where the MAC address is added to the packet. APR and RARP protocol is use to get MAC address of next hop device on the network.

Data link layer push this packet to physical layer where it is sent as stream of “0” and “1” on physical medium available.

and the same process occurs at the destination but in a reverse order.

So I have explain the process without going too much in the deep.

Let’s Get Started

let’s get started ….. Its been a week to joined C42 Engineering, Bangalore as a junior consultant. It was amazing experience for me to entering into a corporate world. 1st day when I entered into the office I saw all people are standing in a front of laptop screen I also joined that, everybody was explaining their progress on projects. I also introduced myself to everyone actually it was “daily stand-up meeting” A daily stand-up meeting is a short organizational meeting that is held each day. The meeting, generally limited to between five and fifteen minutes long, is sometimes referred to as a stand-up, a morning roll-call or a daily scrum. The purpose of the meeting is for each team member to answer the three questions : What did you do yesterday? What will you do today? Are there any impediments in your way? and after the stand-up then I met with the colleagues.and I found that everyone was very passionate and dedicated to their work.

that’s all about myself in my further post I’ll be talking about the data structure, other computer networking stuff, software design and agile methods.