EMC Interview Question
Software Engineer in TestsTeam: RSA
Country: India
Interview Type: Written Test
Recursive calls are considered to be implicit loops, so this is not a solution that meets the constraints (w/o loops)
Just about everything is an implicit loop of some kind. What other solutions are we to offer? We could have a C++ constructor that increments a global variable each time it runs and then create an array of 500 such objects, like: public Foo() { i++; cout << i; cout << endl; } Foo myFoo[500]; That would also be an implicit loop. If we deny every "implicit loop", we're going to have to go with cout << 1; cout << 2; cout << 3; ... hehe
I think recursion was probably the expected solution.
/**
* Without using loops, write a function to
* print 1 to 500 in serial order.
**/
public class RecursionExample {
public void recursion(int num) {
int i = 501-num;
System.out.print(i+" ");
if (num > 1) {
recursion(num-1);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
RecursionExample re = new RecursionExample();
re.recursion(500);
}
}
Here is the pseudo code that works with the MPLAB v8.83 & XC16 Compiler from Microchip:
#include <p30fxxxx.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
int functer (int z)
{
while(z<=500)
{
printf("%d\n",z++);
functer (z);
}
exit(0x0);
}
int main (void)
{
volatile int ze;
functer(0x1);
return ze;
}
call as print(500)
- bidhan.cstbesus June 02, 2012