Jane Street Interview Question for Software Engineer / Developers

Country: United States
Interview Type: Phone Interview

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

type 'a tree = 
    | Leaf of 'a 
    | Tree of 'a tree list

let flatten tree = 
    let rec aux root acc = match root with 
        | [] -> acc
        | Leaf x::t -> aux t (x::acc)
        | Tree h::t -> aux t (aux h acc)
    in List.rev(aux tree []);;

flatten [ Tree [ Leaf 1; Leaf 2 ; Tree [ Leaf 3 ; Tree [ Leaf 4 ]]]];;

OCaml and other functional languages were made for this problem.

In a C program, I would use a qualified union, which is the alternative in an imperative language.

typedef struct _Tree {
      int tell;
      int branch_count;
      union _Data {
            int num;
            struct _Tree *branches;
      } Data;
} Tree;


I believe the size of the union is the size of the largest of its elements, where the union can only be one of the elements at any one time; hence, it can either be a pointer to its first branch, or it can be a leaf's data. The tell int determines if it is a leaf or a node with branches. If it does have branches, num_branches determines how many.

- panoptic.biopower December 14, 2012 | Flag Reply

Add a Comment

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


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


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