Interview Question for Software Engineer / Developers


Country: India




Comment hidden because of low score. Click to expand.
2
of 2 vote

#include<iostream>
#include<vector>
using namespace std;

class A
{
public:
A(int x )
{
cout<<x<<endl;
}

};
int main()
{
int x = 10;
vector<A> a(1000,x);


}

- Anonymous April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

ingenious...

- Deepak May 03, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

Doesn't vector initialization use loop inside?

- Murali Mohan June 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 3 vote

I know this is lame, but here is what I could think of:

public class Print100Times {

	int i=0;
	
	public void method1(int elementToBePrinted)
	{
		if(++i<=100)
		{
			System.out.print(elementToBePrinted);
			method2(elementToBePrinted);
		}
	}

	private void method2(int elementToBePrinted) {
		if(++i<=100)
		{
			System.out.print(elementToBePrinted);
			method1(elementToBePrinted);
		}
	}
	
	public static void main(String[] args)
	{
		Print100Times p=new Print100Times();
		p.method1(0); 
	}
}

There is no loop and technically speaking there is no recursion either. :P

- teli.vaibhav April 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is recursion in method2.

- Nitin Gupta April 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Apologies. I intended to call method1, has been edited now.

- teli.vaibhav April 20, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I too came up with the same method ... (I kind of agree on the "lame" part :p).
However, is it still considered recursion ?

- kant April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 votes

Nope it is not recursion for sure..

- teli.vaibhav April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That is recursion. The fact that it uses two methods does not affect the fact that it is a recursive algorithm.

- Alex April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

Feel free to propose a better solution..

- teli.vaibhav April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is recursion. There are 2 types of recursion.
One calling inself and the other calling each other
One is direct recursion and the other is indirect recursion

- DashDash April 23, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{
int i=1;
label:
if(i<=1000)
{
printf("%d ",i++);
goto label;
}
return 0;
}

- venkatesh ch April 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think "Goto" is technically a loop.

- Alex April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Toggle between functions:

#include<iostream>
using namespace std;

void func1(int n, int k);
void func2(int n, int k);

void func1(int n, int k) {
     if(n>0) {
             cout<<k<<endl;
             n--;
             func2(n, k);
     }
}

void func2(int n, int k) {
     if(n>0) {
             cout<<k<<endl;
             n--;
             func1(n, k);
     }
}

int main() {
    func1(1000, 10);
    system("pause");
    return 0;
}

- Anubhav April 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is considered recursion as well

- adrienconrath April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can also use "goto" for these

- Anonymous April 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Are we talking about loops at the assembly level?

When building this with g++ -std=c++11 -ftemplate-depth=1000, this generates a binary without loops.

#include <iostream>
#include <functional>

typedef std::function<void(unsigned i)> CallbackFn;

template<unsigned i>
struct Looper
{
  static void loop(CallbackFn fun)
  {
    fun(i);
    Looper<i - 1>::loop(fun);
  }
};

template<>
struct Looper<0>
{
  static void loop(CallbackFn fun)
  {
  }
};

int
main (int argc, const char* argv[])
{
  int var = 42;

  Looper<1000>::loop(
    [var] (unsigned i)
    {
      std::cout << var << std::endl;
    }
  );
  return 0;
}

- adrienconrath April 21, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Found it out from GeeksForGeeks

#include <iostream>
using namespace std;
 
template<int N>
class PrintOneToN
{
    public:
           static void print()
    {
        PrintOneToN<N-1>::print();  // Note that this is not recursion
        cout << N << endl;
    }
};
 
template<>
class PrintOneToN<1>
{
public:
    static void print()
    {
        cout << 1 << endl;
    }
};
int main()
{
    const int N = 100;
    PrintOneToN<N>::print();
    return 0;
}

- Sudhindra.A April 22, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is dynamic recursion :)

- Anonymous April 22, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I find a type called "Dynamic Recursion" under Recursion Types.
Can you give me the link for its definition?

- Sudhindra.A April 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I didnt find a type called "Dynamic Recursion" under Recursion Types.
Can you give me the link for the same?

- Sudhindra.A April 24, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

I didnt find a type called "Dynamic Recursion" under Recursion Types.
Can you give me the link for the same?

- Sudhindra.A April 24, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

use a Windows Form timer then it gets really easy

- Stephen May 02, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More