Friday, March 30, 2012

Directi internship questions

    Hello world! Sorry didn't publish anything since long, I am lazy and you know it.
        Anyways, due to our performance at ICPC Asia regionals at Amritapuri, I got an internship offer from Directi (big deal :) ). As a part of the interview procedure I had to give a phone interview totally based on Algos. I was asked following 2 questions:
    1. Given a number, find the smallest number larger than the given number and consists of the same
        digits as the given number.
        i.e if input = 123 then the answer would be 132
       
        Soln - Start from the last digit, move till u find a digit smaller than the last digit and then place the last      
                  digit in front of the found digit and then sort the digits present on it's right hand side.

     2. Given an array ,for all members x find a number y such that:
             y<x and pos(y) < pos(x) and pos(y) is closest to pos(x) [pos(x)=position of x in the array]
         i.e given 1,2,5,3,4 answer will be 2:1, 5:2, 3:2, 4:3

         Soln: Stack comes in handy over here.
                   Place the first number in stack.
                   Now for every member check the top of the stack. If it is smaller, then the number is your    
                   answer, or else, pop the top and again check.
                   Once, you find your answer, push the current number also in the stack.

  

Monday, March 5, 2012

Linux Cron Jobs

  I have always envied Iron man for Jarvis and all other cool toys that he has. I used to envy all those people who had some or the other type of automation in their life.
  Even I wanted some automation through my computer. First I had thought of making a twitter bot that would tweet the news of the day from Wikipedia. I had also a php script to scrap the necessary data . But, then I got bored of the idea and hence dropped it in middle (nothing new for me).
  I come to my room everyday at 12:20 from my college. So, I thought, how about getting my media player ready by the time I reach my room? (Cool Idea huh!)
  I already knew that I had to simply create a cron-job for this. The question was "how?". Well, what is Google for? A simple Google search told me that I just had to enter the command
     crontab -e
  It will ask you for your favorite editor. Select the one of your choice. It opens a user cron file. Now, for people who don't know what cron file is, well in simple words, you enter the time and command and then your computer runs that command on that time. The format of a cron file is:


   m h dom mon dow command
   m-minute
   h-hour
  dom-date of month
  mon-month
  dow- date of week
  command - command to be run

  you can enter * at any field (except command ofcourse!) to run that command every minute,hour etc.
  you can also input range in the form [1-5]
  also, you can give an interval like */2 i.e occur every 2 minutes etc.
  Enter the proper value in each field and enter the correct command. And then save the file. Thats it! You have now a command that will run automatically!
  One thing though, the output of the command will not be shown on the screen but will be mailed to you.
   Also, an important thing: If you are starting any GUI application you need to give command in this format:
   env DISPLAY=:0 command
   e.g env DISPLAY=:0 firefox

   Now, who is going to keep his/her morning alarm using a cron-job? :)