Jet Interview Question for Backend Developers


Country: United States
Interview Type: Phone Interview




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

Explanation:

We simply make `f` ignore its argument and constantly produce a fibonaci function:
`f(y) = fib`.
Then, `Y(f) = f(Y(f))=fib`.

Code:

public class AppLabmdaSubstitution {

	public static Function<Integer, Integer> Y(Function<Function<Integer, Integer>, Function<Integer, Integer>> f) {
		return x -> f.apply(Y(f)).apply(x);
	}

	public static void main(String[] args) {
		//Write your code here
		Function<Integer, Integer> fib = Y(
				func -> x -> {
			if (x < 2)
				return x;
			else
				return func.apply(x - 1) + func.apply(x - 2);
		});

		IntStream.range(1, 11).mapToObj(Integer::valueOf).map(fib).forEach(System.out::println);
	}
	
	/**
	* Function<Integer, Integer> fibonaci = new Function<Integer, Integer>() {
		@Override
		public Integer apply(Integer t) {
			if (t<2) {
				return t;
			} else {
				return apply(t - 1) + apply(t - 2);
			}
		}
		
	};
	*/
}

- Jason April 07, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

c# implementation.

using System;

namespace Fib {
    class Program {

        public static Func<int, int> Y( Func<Func<int, int>, Func<int, int>> f ) {
            return x => f( Y( f ) )( x );
        }

        static void Main(string[] args) {

            var resFunc = Y( func => {
                return new Func<int, int>( i => {
                    int fib0 = 0;
                    int fib1 = 1;
                    int resFib = 1;
                    for ( int j = 1; j < i; j++ ) {
                        resFib = fib0 + fib1;
                        fib0 = fib1;
                        fib1 = resFib;
                    }
                    return resFib;
                });
            });
            var res = resFunc.Invoke( 7 );
            Console.WriteLine( res );
        }
    }
}

- zr.roman January 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

LOL, this made my day.

- bobrobob January 21, 2016 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
    {
        public static Func<int, int> Y(Func<Func<int, int>, Func<int, int>> f)
        {
            return x => f(Y(f))(x);
        }
     
        static void Main(string[] args)
        {
          ///Fib 5	
            Console.WriteLine( Y(ff => x =>x<1?1:ff(x-1)+ff(x-2))(5));
          ////Fib 10  
	  Console.WriteLine( Y(ff => x =>x<1?1:ff(x-1)+ff(x-2))(10));
            
        }
    }

- Anonymous January 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class Program
    {
        public static Func<int, int> Y(Func<Func<int, int>, Func<int, int>> f)
        {
            return x => f(Y(f))(x);
        }
 
        static void Main(string[] args)
        {
            //Fib 5
	    Console.WriteLine( Y(ff => x =>x<1?1:ff(x-1)+ff(x-2))(5));
             //Fib 10
	     Console.WriteLine( Y(ff => x =>x<1?1:ff(x-1)+ff(x-2))(10));
           
        }
    }
}

- AlexDovgan January 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

My solution is

class Program
    {
        public static Func<int, int> Y(Func<Func<int, int>, Func<int, int>> f)
        {
            return x => f(Y(f))(x);
        }
 
        static void Main(string[] args)
        {
            //Fib 5
	    Console.WriteLine( Y(ff => x => x<1 ? 1 : ff(x-1)+ff(x-2))(5));
             //Fib 10
	     Console.WriteLine( Y(ff => x => x<1 ? 1 : ff(x-1)+ff(x-2))(10));
           
        }
    }
}

- alex.dovgan January 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is there in this question. Its plain C# question if a candidate knows Anon and Lambda function.. its just copy that recursive fibo code in return dude..

- hprem991 January 23, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C++:

typedef function< int(int) > int_to_int;

int_to_int Y( function< int_to_int(int_to_int) > f )
{
  return [f](int n) -> int { return f(Y(f))(n); };
}

function< int_to_int(int_to_int) > f = [](int_to_int g) -> int_to_int { return [g](int n) { return n<= 1 ? 1 : g(n - 1) + g(n - 2); }; };

- Anonymous January 28, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In C++:

typedef function< int(int) > int_to_int;

int_to_int Y( function< int_to_int(int_to_int) > f )
{
  return [f](int n) -> int { return f(Y(f))(n); };
}

function< int_to_int(int_to_int) > f = [](int_to_int g) -> int_to_int { return [g](int n) { return n <= 1 ? 1 : g(n - 1) + g(n - 2); }; };

- anonymous January 28, 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