My interview at Mentor Graphics

Mentor Graphics  is a US based multinational company that deals with electronic design and electrical and hardware based software  related services. I was recently called for interview in its Lahore office after being short listed. It was a technical interview and for job related to Embedded system so most of the questions were about either embedded systems or C language and hardware.hh
Their email described following things


You will be assessed as per following criteria:
  1.  Good programming experience in C and or/Java
  2. Good understanding of hardware Architecture (x86, ARM or MIPS) and Operating Systems Concepts 
  3. Hands-on experience with either of following embedded development environment would be a plus – Keil, RealView MDK, IAR Embedded Workbench, Code Composer Studio, Sourcery Codebench

Following are some questions and answers asked form me by interviewer.



  1. What is Cache memory?
    It is a faster type of memory used in computers while CPU is processing. So contents of memory is first fetched in this memory so that future requests can be served faster.
  2. What is its difference with other memories? Why don't we use RAM as its faster too.
    Cache is much faster than RAM and its is used to make the process fast. It is much costly than other memories. RAM is primary memory and different from RAM.
  3. What are Registers in Embedded systems?
    Registers are type of memory that is used to hold the contents that are going to use in processor. They may hold instructions, variables and things like that.So if CPU wanted to add two numbers, instructions will be first loaded into registers and then given to CPU.
  4. How many registers are there in ARM Cortex M3?
    It has 12 general purpose registers out of which 6 are called low level and other 6 are called high level. Than 4 Control registers PC, LR, SP and . Then two Special purpose registers like Program status register.
  5. Suppose we have a 32 bit register whose first and second bit are used to on and off UART. So if bit 8 is 1,Tx channel is on and if  10 bit is 1, Rx is on. How will you do it?
    By and ORing that with a number. So to set bit 8, we will and that register as follows.
    UART_Reg |=h0000100; // for 8th bit
    UART_Reg |=h0000400; // for 10th bit
  6. How can you do in same instruction?
     UART_Reg |=h0000500; // for 8th and 10 bit
  7. Suppose we have a 32 bit register showing status of a 32 interrupts of 32 ADC channels. Now Suppose a battery is connected at an xth channel. Write a efficient C program that can detect channel Number and return it.
    1. int ChannelFinder(int INT_Reg)
    2. {
    3.     int ch_No,i,temp;
    4.     temp=INT_Reg;
    5.     for(i=0;i<=31;i++)
    6.         {
    7.           temp>>i;
    8.           if(temp==1)
    9.             {
    10.             ch_No=i;
    11.             break;
    12.             }  
    13.            
    14.         }
    15.         return ch_No;
    16. }

  8. Now suppose that more than on interrupts are occurring . Also in the if condition call another routine that will call ISR with Ch_No. Calling this routine will also clear previous Interrupt.
    1. int ChannelFinder(int INT_Reg)
    2. {
    3.     int ch_No,i,temp;
    4.     temp=INT_Reg;
    5.     for(i=0;i<=31;i++)
    6.         {
    7.           temp>>i;
    8.           temp |=1;
    9.           if(temp==1)
    10.             {
    11.             ch_No=i;
    12.            ISR_funct(ch_No);
    13.             break;
    14.             }  
    15.            
    16.         }
    17.        
    18. }
9. This program is checking for interrupt even if there is no interrupt, change it in such a way that it only checks when there is  some interrupt?
  1. int ChannelFinder(int INT_Reg)
  2. {
  3.     int ch_No,i,temp;
  4.     temp=INT_Reg;
  5. if(INT_Reg>=1)
  6. {
  7.     for(i=0;i<=31;i++)
  8.         {
  9.           temp>>i;
  10.           temp |=1;
  11.           if(temp==1)
  12.             {
  13.             ch_No=i;
  14.            ISR_funct(ch_No);
  15.             break;
  16.             }  
  17.            
  18.         }
  19. }
  20. else
  21. break;
  22.        
  23. }
 
  10. See following program

         const int x=9;
        What will happen if i tried to change value of x?
         It will give and error.

   11. Now see this
            int *p;
         p=&x;
        *p=10;
       What will happen in this case?
      Again same error.

   12. What will be output of following?
      
  1.       int *p;
  2.       sizeof(p);
  3.       double *p1;
  4.       sizeof(p1);
      Both pointer will have size of 4 bytes as size of addresses in 32      bit machine is 4 bytes.

  13. If both have same sizes, why can't we use int type pointer for        double type?
     'cause a double type pointer is pointing a double and int type is        pointing int. [Not sure]

  14. How you configured timer in your project?
       In data sheet of the controller, sequence to set bits of relevant          register were given. There were  some parameters like                      overflow  value etc were to be set to start that timer. 
       That registers   were set and it was configured.

  15. How you used timer?
      Timer was used to produce delays and also to sample data at             regular intervals. So We used Sys Tick timer which is 24                   bit timer. We used 80Mhz clock. It was count up timer. Each bit       was count  up after the delay of (1/80M)s so routine was                   made to produce of specific intervals.

 16. What if you can't set value of a timer and you have to produce          some specific interval?
       Then we can set it Reload value. So every time overflow                  occurs,      it will reload with a value relevant     to our delay.

17. Following is a c Code
     
  1. struct A{
  2.      int var1;
  3.      int *p1;
  4.      int *p2
  5.      };
     How can i declare array of struct A type?
     struct A a1[4]; [He was suspicious about this, he said it will be       then struct of struct, I think he was confused with Cpp]

 18. Now what will happen if i do following a1++?
       [I was confused here, We had argument. He tried to convince           me something may be he was  trying to trap me.                               Unfortunately, I got into trap and I think answered wrong ]
      It will be a compiler error.

 19. What is pipeline in MCU's?
      It is a concept that while one instruction is being loaded, other         will also be loaded to save time.  He asked another                           question but  I said that I dont know deep about this so...

  20. What is const int *p and int int const *p who will be constant           in these?
      It is a famous question in interviews of C. It can be found here.
      http://stackoverflow.com/questions/1143262/what-is-the-                 difference-between-const-int-const-int-    const-and-int-const

21. By what steps program is compiled? [He asked it differently but       I don't remember exact words]
    http://stackoverflow.com/questions/3996651/what-is-compiler-linker-loader

21. What is difference between big endian and little endian?
       Both are ways of storing memory. In little endian, LSB is                  stored in smaller address place and MSB is placed in higher              address places. Vice versa is true in other.

22. Suppose we have a system, a black box, we don't know                     anything about it. How can you know weather its big endian or       little endian?
     We can write a simple program as following
            char var=1;
     int c;
     c=(int) var;
     As we know int is a 4 byte and char is one byte. Now when type     casted, c will have 4 bytes. So if it will be equal to 1 it means      little endian other wise big endian.

23.What is interrupt and what happens when it occurs?
   An interrupt is blockage in flow of main program. I told him               whole process of interrupts what  I          have read in ARM       with all the details.

24. So how NVIC know ISR address.
   I told him that it IPSR register where interrupt number is loaded      form NVIC. All the ISR's are            stored in a memory address       from 0 to top. So after this PC loads that specific  interrupt. But        he was    not convinced.

25. Following is a program
  
  1.  if( Unknown Condition )
  2.       {
  3.      printf("Hellow");
  4.               }
  5.   else
  6.    printf("World");
 how to print Hellow world? What should we write in If condition.

We can write  ~(printf("Hellow")). As return type of printf is number of bytes being printed. So what if condition will be false but print function will print Hellow and then it will go to else and will print World.

  1.  if( ~(printf("Hellow")) )
  2.       {
  3.      printf("Hellow");
  4.               }
  5.   else
  6.    printf("World");

26. Suppose data of an automotive is stored in location like following
 Now suppose at location 0-3 car reg number is stored, on 4-9 something else is saved and so on and so far. Write a C program that takes starting bit, and size and extracts all of them and returns.


  1. int Extractor(int start, int size)
  2. {
  3.     int i,cutter=0;
  4.     int *adres;
  5.     adress=start/32;
  6.    
  7.     for (i=start;i<=size;i=2*i)
  8.     {
  9.         cutter=cutter+i;
  10.     }
  11.    
  12.     return ((*p)&cutter);
  13. }

[I was a bit wrong in this. I told him right algorithm but I was failed to produce right c code for cutter]


27. Suppose a MCU has only 12 registers while a C program is using 20 variables in it. All of them at once. How registers will manage it?

Register are only there to speed up the process of CPU by providing immediate data. In fact these variables are stored in memory. One other way is pushing and then poping when required in stack. 

It was very interesting interview. I imagined it as two nerds discussing most nerdy things. We argued a lot of time during interview. Overall it was best experience.  

Evaluation of 7 Scientific Computing Methods

Engineering is basically nothing but solving problems. Problems are  often solved in systematic ways using theory and experiments combined. Most engineering problems involved mathematical modeling of the system to ease whole process of solving. This mathematical modeling often ends up having a lot of  big, scary equations that needed to be solved. Traditionally these equations were solved using pen , pencils and brain alone. So it was lengthy and error prone process. For example, in past if some company needed to install grid station or  transmission lines, all the analysis was done with hands. It was a tedious, time consuming and error prone process. But with the advent of computer, whole process has become very easy. Now a lot of simulation programs are available that can help us develop our systems without even understanding underlying theories and mathematical modeling. These software first convert data into mathematical models and then solve them using different iterative and other Numerical techniques.
In computational techniques, mathematical problems are first converted into   computer's language and then solved. One such way is iterative methods. In iterative methods, a problem is solved with some  specific computer based formulas repeatedly un till answer in specific range of error comes. In these methods,  some rough guesses are made and then by the help of algorithms, problems are solved  iteratively until  error is within range. Although these methods don't give 100% exact answers, but still they are used. As in mathematical modeling , answer with some errors can be used.
We have developed some computational methods to solve non- linear equations in MATLAB. In following paragraphs, we tried to compare them with different parameters.
First equation

f(x) = e-x - x
Bracketing methods
Bracketing methods are solved with two guesses such that root of the equation lines in between these two methods. In these methods convergence is assured but they take more time and iterations to solve equations.

This is one such example where f(x) is a function while blue dots are initial guess and black dot is root of the function.
1) Bisection
It is also called incremental search as in each iteration, interval is halved.
A simple formula is used in this case.
Xnew=(x0+x1)/2




See how error is first decreasing very fast and then speed becomes slow and ultimately it becomes constant.
2) False Position Method
This method is also bracketing but its algorithm is different. It joins f(xlower) and f(upper ) with straight line. Then this line's intersection with x axis is located and this is used as new guess untill error becomes very small.
Formula for this method is

This method is much improved w.r.t. to Bisection. It uses less number of iterations and less time. So it converges more rapidly.


3)Modified False Method
This method is also identical to False position, In fact it is modified version of False position. Only modification made here is that if same side of interval is used more than once, then value of that function is used by dividing 2 to power number of times same interval is used.  It should converge faster than False position.


Open Methods:
Close methods are more accurate , they require only one  guess and mostly requires less time(Not true always). One big advantage of this method is that we can use almost any guess although a more rough guess will make it harder and slower to find solution.
It may or may not converge. So these methods can't be used for any equations.
4) Fixed Point Iteration
This is simples method of all these. In this method equation is rearranged and one x is taken out then value is substituted and iterations are performed.  So for a function f(x) , we use x=g(x) and initial guess is substituted.  This method is simplest but also slowest. It requires more number of iterations then any of the following. Time required for this method greatly depends upon function.

5)  Secant Method                                
Secant method is like false position method but it don't requires  close interval.  It actually uses Newton Raphson method with derivation's approximation as follows

Formula for this method is as follows

This method is fast like Newton Raphson but requires much less time as derivation is a big problem to evaluate in computers. Note that it uses much less iterations and time compared to other methods describes so far.


6) Improved Secant Method
It is a improved version of secant method. Small perturbation  change is used in this method. as follows





Note that it requires less time and no of iterations.


7) Newton Raphson
In this method, a tangent is drawn from the point of initial guess and extended to x-axis. Intersection with x-axis provides next guess and this way it is solved iteratively.
Formula for this method is as follows





Note that it requires much more time than any other method.
Conclusion:
Time of Execution:
For time point of view Modified Secant method is best and Newton Rapshon is worst.


Number of iterations used
On this base Newton Raphson and Modified Secant methods are best and Fixed point iteration is worst as expected. It is often observed that newton Raphson requires less no of iterations but more time.

On average, Modified Secant Method is best among all.

References:

Applied Numerical  Methods with MATLAB for Engineers 3ed by

Steven C. Chapra