Tesla Interview Question: write a command-line program ... | Glassdoor

Interview Question

Software Engineer Interview Palo Alto, CA

write a command-line program to evaluate a set of

  equations. Like: a=b+4 b=c+d d=4 c=3+2
Answer

Interview Answer

3 Answers

0

C++ code for a topological sort

Interview Candidate on Oct 21, 2017
1

This can be a simple as a toposort, or as hard as a Gaussian Substitution depending on the requirements

Anonymous on Feb 16, 2018
0

import (
    "os"
    "fmt"
    "strings"
    "strconv"
)

func calc(equations map[byte]string, key byte) int {
    expr := strings.Split(equations[key], "+")
    sum := 0
    for _, ex := range expr {
        val, err := strconv.Atoi(ex)
        if err == nil {
            sum += val
        }
    }
    return sum
}

func resolve(equations map[byte]string, key byte) string {
    buf := []byte(equations[key])
    for i:= 0; i= 'a' && v <= 'z' {
            _, ok := equations[v]
            if ok {
                equations[v] = resolve(equations, v)
                buf = append(buf[0:i], append([]byte(equations[v]), buf[i+1:]...)...)
            }
        }
    }
    return string(buf)
}

func main() {
    args := os.Args[1:]
    equations := make(map[byte]string, len(args))
    for _, arg := range args {
        parts := strings.Split(arg, "=")
        equations[parts[0][0]] = parts[1]
    }
    for key, _ := range equations {
        equations[key] = resolve(equations, key)
        fmt.Printf("%c=%d\n", key, calc(equations, key))
    }
}

Anonymous on Dec 13, 2018

Add Answers or Comments

To comment on this, Sign In or Sign Up.