unknown Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

task is written quite unclear.
As I understand it, the implementation could be the following (c#).
No parsing logic, only data structure and string representation.

using System;
using System.Collections.Generic;

namespace TreeDataStructure {
    
    public class TreeNode {

        public Operation? Operation { get;  }
        public string Operand { get; }
        public List<TreeNode> Children { get; }
        public TreeNode Parent { get; private set; }

        public TreeNode( Operation? operation, string operand = null ){
            Operation = operation;
            Operand = operand;
            Children = new List<TreeNode>();
            Parent = null;
        }

        public void AddChild( TreeNode child ) {
            Children.Add( child );
            child.Parent = this;
        }

        public void AddChildren( params TreeNode[] children ) {
             foreach ( var child in children ) {
                Children.Add(child);
                child.Parent = this;
            }
        }
    }

    public static class TreePrinter {

        private static string _expression;

        public static string GetStringRepresentation( TreeNode anyNode ) {

            var startNode = _getStartNode( anyNode );
            _printTree( startNode );
            return _expression.TrimEnd(", ".ToCharArray());
        }

        private static TreeNode _getStartNode( TreeNode anyNode ) {

            if (anyNode.Parent == null) {
                return anyNode;
            }
            return _getStartNode( anyNode.Parent);
        }

        private static void _printTree( TreeNode startNode ) {

            if ( startNode.Operand == null ) {
                _expression += startNode.Operation + "(";
                
                for(int i = 0; i < startNode.Children.Count; i++ ) {
                    _printTree( startNode.Children[ i ] );
                    if ( i != startNode.Children.Count - 1 && startNode.Operation == null ) {
                        _expression += ", ";
                    }
                }
                _expression += "), ";
            }
            if (startNode.Operation == null) {
                _expression += startNode.Operand  + ", ";
            }
            _expression = _expression.Replace(", )", ")");
        }
    }

    public enum Operation{
        AND,
        OR,
        NOT
    }

    class Program {

        static void Main( string[] args ) {
//Test case
// ((name = "smith" OR name = "Johnes") AND age > 9) OR (age < 8 AND name != "Williams") OR ( Not(city = "New York") OR city = "London" )
// return representation:
// OR(AND(OR(name = "Smith", name = "Johnes"), age > 9), AND(age < 8, name != "Williams"), OR(NOT(city = "New York"), city = "London"))

            TreeNode node212 = new TreeNode(null, "name = \"Johnes\"");
            TreeNode node211 = new TreeNode(null, "name = \"Smith\"");
            TreeNode node21 = new TreeNode( Operation.OR );
            node21.AddChildren( node211, node212 );

            TreeNode node22 = new TreeNode(null, "age > 9");
            TreeNode node2= new TreeNode( Operation.AND );
            node2.AddChildren( node21, node22 );

            TreeNode node31 = new TreeNode(null, "age < 8");
            TreeNode node32 = new TreeNode(null, "name != \"Williams\"");
            TreeNode node3 = new TreeNode( Operation.AND );
            node3.AddChildren( node31, node32 );

            TreeNode node411 = new TreeNode(null, "city = \"New York\"");
            TreeNode node41 = new TreeNode( Operation.NOT );
            node41.AddChildren( node411 );

            TreeNode node42 = new TreeNode(null, "city = \"London\"");
            TreeNode node4 = new TreeNode( Operation.OR );
            node4.AddChildren( node41, node42 );

            TreeNode node1 = new TreeNode( Operation.OR );
            node1.AddChildren( node2, node3, node4 );

            Console.WriteLine(TreePrinter.GetStringRepresentation( node31 ) );

            Console.ReadLine();
        }
    }
}

- zr.roman November 19, 2015 | 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