Interview Question for Software Engineers


Country: United States
Interview Type: In-Person




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

package main

import (
	"fmt"
)

type Tree struct {
	val   int
	left  *Tree
	right *Tree
}

func findSequence(root *Tree) []int {
	lTry := findSequenceHelper(root.left)
	rTry := findSequenceHelper(root.right)
	var res []int
	if len(lTry) > len(rTry) {
		res = lTry
	} else {
		res = rTry
	}
	cTry := findSequenceHelper(root)
	if len(cTry) > len(res) {
		res = cTry
	}
	return res
}

func findSequenceHelper(root *Tree) []int {
	res1 := lessSequence(root.left, root.val)
	res1 = append(res1, root.val)
	res1 = append(res1, graterSequence(root.right, root.val)...)
	res2 := graterSequence(root.left, root.val)
	res2 = append([]int{root.val}, res2...)
	res2 = append(res2, lessSequence(root.right, root.val)...)
	if len(res1) > len(res2) {
		return res1
	} else {
		return res2
	}
}

func lessSequence(root *Tree, max int) []int {
	if root == nil || root.val != max-1 {
		return []int{}
	}
	lTry := lessSequence(root.left, root.val)
	rTry := lessSequence(root.right, root.val)
	if len(lTry) > len(rTry) {
		return append(lTry, root.val)
	} else {
		return append(rTry, root.val)
	}
}

func graterSequence(root *Tree, min int) []int {
	if root == nil || root.val != min+1 {
		return []int{}
	}
	lTry := graterSequence(root.left, root.val)
	rTry := graterSequence(root.right, root.val)
	res := []int{root.val}
	if len(lTry) > len(rTry) {
		return append(res, lTry...)
	} else {
		return append(res, rTry...)
	}
}

func main() {
	root := &Tree{
		val: 2,
		left: &Tree{
			val: 1,
			left: &Tree{
				val: 0,
				left: &Tree{
					val: 10,
				},
				right: &Tree{
					val: -1,
				},
			},
		},
		right: &Tree{
			val: 3,
			left: &Tree{
				val: 4,
				right: &Tree{
					val: 5,
					left: &Tree{
						val: 6,
					},
				},
			},
			right: &Tree{
				val: 7,
			},
		},
	}
	fmt.Println(findSequence(root))
}

- dmitry.labutin February 26, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i think you are considering only those path which includes root. However in the question, it's mentioned that root is not required. Please confirm. I could not run your code, since I don't know which language is this.

Please confirm if I am wrong.

- snehasishroy39 March 01, 2018 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

<?php

$binary=[2=>2];

$binary[2]=[1=>1,3=>3];

$binary[2][1]=[0=>0];
$binary[2][1][0]=10;

$binary[2][3]=[4=>4,7=>7,5=>5];


echo '<pre>';
print_r($binary);
$result=array_unique(itrate($binary,[]));
//array_unique(($binary);
sort($result);

calculateLongSeq($result);


function calculateLongSeq($arr){

$max_seq=[];
$seq=$arr[0];
$count=0;
for($i=0;$i<count($arr);$i++){

if($seq==$arr[$i]){
$count++;
$seq++;
}
else{
$max_seq[]=$count;
$count=0;
$seq=$arr[$i];
}
}

echo max($max_seq);
exit();
}

function itrate($binary,$result){

foreach ($binary as $key => $value) {

array_push($result,$key);

if(is_array($value)){
$result=array_merge($result,itrate($value,$result));
}
else{
array_push($result,$value);
}
}

return $result;
}

- php solution February 27, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

<?php

$binary=[2=>2];

$binary[2]=[1=>1,3=>3];

$binary[2][1]=[0=>0];
$binary[2][1][0]=10;

$binary[2][3]=[4=>4,7=>7,5=>5];


echo '<pre>';
print_r($binary);
$result=array_unique(itrate($binary,[]));
//array_unique(($binary);
sort($result);

calculateLongSeq($result);


function calculateLongSeq($arr){

	$max_seq=[];
	$seq=$arr[0];
	$count=0;
	for($i=0;$i<count($arr);$i++){
	
		if($seq==$arr[$i]){
			$count++;
			$seq++;
		}
		else{
			$max_seq[]=$count;
			$count=0;
			$seq=$arr[$i];
		}
	}

	echo max($max_seq);
	exit();
}

function itrate($binary,$result){
	
	foreach ($binary as $key => $value) {

		array_push($result,$key);
		
		if(is_array($value)){
			$result=array_merge($result,itrate($value,$result));
		}
		else{
			array_push($result,$value);
		}
	}

	return $result;
}

- php solution February 27, 2018 | 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