Jet Interview Question for Backend Developers

• 0

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) {
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);
}
}

};
*/
}``````

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 );
}
}
}``````

Comment hidden because of low score. Click to expand.
0

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));

}
}``````

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));

}
}
}``````

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));

}
}
}``````

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..

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); }; };``````

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); }; };``````

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.

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.