## Swiggy Interview Question for SDE-3s

Country: United States

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

``````def get_node(value, nodes){
if ( value !@ nodes ){
node = { 'v' : value, 'c' : list() , 'p' : null }
nodes[value] = node
}
nodes[value] // return
}

def create_graph( list_of_pairs ){
nodes = dict()
for ( pair : list_of_pairs ){
#(parent,child) = pair
p_node = get_node( parent, nodes )
panic ( size( p_node.c ) > 1 , "It supposed to be having only 2 child!" )
c_node = get_node( child , nodes )
assert ( empty( c_node.p ) , "Duplicate - or - multiple parent for a child!" )
c_node.p = p_node.v // parent the child
p_node.c += c_node.v // child the parent
}
nodes // return
}

def find_root_for_valid_bin_tree(graph){
// check if graph valid binary tree?
possible_roots = select( graph ) where {
n = \$.o.value
empty(n.p )
}
assert( size(possible_roots) == 1, "Is a graph, with multiple roots" )
// check for cycles ? easy... do dfs and check ...
root = possible_roots[0]
stack = list()
stack.push( root.key )
visited = set()
while (!empty(stack) ){
nv = stack.pop()
panic( nv @ visited , "There are cycles!" )
visited += nv
for ( c : graph[nv].c ){ stack.push( c ) }
}
root.key // return root
}

def str_bin_tree( root_value, graph ){
root = graph[root_value]
left_str_rep = str_bin_tree( root.c[0] , graph  ) ?? ""
right_str_rep = str_bin_tree( root.c[1] , graph ) ?? ""
str( "(%s%s%s)", root_value, left_str_rep, right_str_rep )
}

def do_swiggy(list_of_pairs){
g = create_graph(list_of_pairs)
r = find_root_for_valid_bin_tree(g)
println( str_bin_tree( r , g ) )
}

pair_list = [ ['A','B'], ['B','C'], ['A','D'], ['C','E']  ]
do_swiggy( pair_list )``````

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

``````public static void main(String[] args) throws Exception {
System.out.print(swiggySDE3(Arrays.asList("(A,B)", "(B,C)", "(A,D)", "(C,E)", "(A,F)")));
}

public static String swiggySDE3(List<String> str) {
Map<String, List<String>> map = new HashMap<>();
for (String t : str) {
String[] pair = t.substring(1, t.length() - 1).split(",");
final List<String> orDefault = map.getOrDefault(String.valueOf(pair[0]), new ArrayList<>());
map.put(pair[0], orDefault);
}
StringBuffer stringBuffer = new StringBuffer();
swiggySDE3UTIL("A", map, stringBuffer);
return stringBuffer.toString().substring(0, stringBuffer.length()-1);
}

private static void swiggySDE3UTIL(String start, Map<String, List<String>> map, StringBuffer stringBuffer) {
final List<String> values = map.get(start);
if (values == null || values.isEmpty()) {
stringBuffer.append(start + ")");
return;
}
stringBuffer.append(start+"(");
values.forEach(t->swiggySDE3UTIL(t,map,stringBuffer));
stringBuffer.append(")");
}``````

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

Simply, maintain two HashMap. Parent Map and child map. In parent map keep a child count. In child map keep a parent info. When inserting new tuple check.

1. parent map already has more than 2 child count.
2. child already has same parent as in tuple
3. child already has a parent.

4. After all tuples are entered run through the parent table and count entries with 0 child if more than 1 flag error.

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.