Interview Question


Country: India
Interview Type: Written Test




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

class fabonacci{
public static void main(String []args)
{
int a=1,b=1,c;
int n=10,d=0;
System.out.println(a);
System.out.println(b);
while(d++<n-2)
{
c=a+b;
if(c%2!=0)

System.out.println(c);
else
System.out.println("#");
a=b;
b=c;
}
}
}

- Saurabh Ahuja June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

public class Fibonacci {

static int count=0;
public static void main(String[] args) {
printFibonacci(1,1);
}

private static void printFibonacci(int i,int j) {
count++;
if(count!=10){
System.out.println(i%2==0?"#":i);
printFibonacci(j, j+i);
}

}
}

- Paras Agarwal June 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

As an optimization, every third number is even so if you can keep track of that you don't have to check if it is divisible by 2.

- jordan July 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is likely what they're looking for.

- tres.kajinski July 02, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not sure whats the point of question. All the answers here are good enough. Tried to do it a different way.

package com.project.euler;

public class StrangeFib {
	public static String result = "";
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		fib(1,1);
		System.out.println(result);

	}

	public static void fib(int n,int m){
		if(n+m>1000)
			return;
		
		result = result + n+", "+m+", # ";
		fib(n+2*m,(n+m)+(n+2*m));
	}
}

- AlgoAlgae July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class TestFibo {

	public static void main(String[] args) {
		int n =10;
		List<String> l = new ArrayList<String>();
		for(int j =0;j<=n;j++){
			if(genarateFibinacci(j)%2 == 0)
				l.add("#");
			else
				l.add(String.valueOf(genarateFibinacci(j)));
		}
		for(String s : l){
			System.out.println(s);
		}
	}
	
	static int genarateFibinacci(int n){
		if(n==0)
			return 0;
		else if(n==1)
			return 1;
		else 
			return genarateFibinacci(n-1) + genarateFibinacci(n-2);
	}
}

- kmlsharma53 July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;

public class TestProgram {

	static final String COMMA = ",";
	static final String START_BRACE = "{";
	static final String END_BRACE = "}";
	static final String HASH = "#";

	public static void main(String[] args) {
		Scanner input = null;
		try {
			input = new Scanner(System.in);
			int totalElements = input.nextInt();
			int num1 = 1;
			int num2 = 1;
			System.out.print(START_BRACE + num1 + COMMA + num2);
			for (int i = 2; i < totalElements; i++) {

				int num3 = num1 + num2;
				num1 = num2;
				num2 = num3;
				System.out.print(COMMA);
				if (isEven(num3))
					System.out.print(HASH);
				else
					System.out.print(num3);
			}
			System.out.println(END_BRACE);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (input != null)
				try {
					input.close();
				} catch (Exception e2) {
					// ignore
				}
		}
	}

	static boolean isEven(int num) {
		return num % 2 == 0 ? true : false;
	}

}

- Mohit Mehta July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

a,b=0,1;
while b<=30:
if b%2 == 0:
print '# '
else:
print b
b,a=a+b,b

- durai July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can generate the Fibonacci number by recursion, then just test if the number going to be printed out is an even number or not.

public class test {

	public static int fibonacci(int i)
	{
		if(i == 1 || i == 2)
			return 1;
		else
			return fibonacci(i-1)+fibonacci(i-2);
	}
	
	
	
	public static void main(String[] args) {
		
		int n;
	   for(int i = 1; i< 11; i++)
	   {
		   n = fibonacci(i);
		   if(n%2 == 0)
		   {
			   System.out.print("# ");
		   }
		   else
		   {
			   System.out.print(n+" ");
		   }
	   }
	}
}

- gzyeli123 July 01, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class fib
{
public static void main(String args[])
{
int f,f1=0,f2=1;
for(int i=0;i<20;i++)
{
f=f1+f2;
f1=f2;
f2=f;
System.out.print(f%2==0?"#":f);
System.out.print(",");
}
System.out.println();
}
}

- Anonymous July 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Fibbonacci {

	
	public static void main(String[] args) {
		int i = 0;
		int x = 1;
		int num = 10;
		int sum;
		System.out.print(i+" "+x+" ");
		for(int j=1; j<num;j++){
			sum=i+x;
			if(sum%2==0)
				System.out.print("#"+" ");
			else
				System.out.print(sum+" ");
			i=x;
			x=sum;
		}
	}

}

- Anonymous July 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Test {
public static void main(String[] args) {
int f1 = -1,f2=1,f3=0;
for(int i=0;i<=10;i++)
{
f3 = f1+f2;
f1=f2;
f2=f3;
if((f3%2)==0)
{
System.out.print("#"+" ");
}
else
{
System.out.print(f3+" ");
}
}
}
}

- chinnaraj October 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class FebonacciwithSymbol {
public static void febonacci(){

int n1=0,n2=1,n3,i,count=12;
System.out.print(n2);

for(i=2;i<count;++i)
{
n3=n1+n2;
if(n3%2==0){
System.out.print("," + "#");
}
else {
System.out.print("," + n3);
}
n1=n2;
n2=n3;
}

}

public static void main(String[] args) {
febonacci();
}
}

- KNSManasa December 08, 2016 | 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