Interview Question


Country: United States
Interview Type: Written Test




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

Parsed input into list of Term object (coefficient and mapping from var name to its power value), recursively parsing inside parentheses. Combined likes by mapping from strings representing terms without coefficients (x2y3z) to List<Term>. Sorted by term's highest power value.

- kanukadze October 18, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

// ZoomBA
def term_parse( s ){
  term = [ 0.0 , '' ]
  p = tokens('^[\+\-]?(\d+(\.\d+)?)?')
  m = p.matcher( s ) ; m.find() // true always 
  opts = { '' : 1.0 , '+' : 1.0 , '-' : -1.0 }
  if ( m.group @ opts ) {
    term[0] = opts[ m.group ] 
  } else {
    term[0] = float( m.group ) 
  }
  inx = index ( s.value ) :: { str($.item) =~ '[a-zA-Z]' }
  term[1] = s[ (inx < 0 ? 0 : inx) : -1 ] 
  if ( inx < 0 ){ term[1] = '' }
  term 
}
def gen_expr( s ){
  l = tokens(s, '[\+\-]?[^\+\-]+') -> { term_parse( $.item ) }
  d = mset ( l ) -> {  $.item.1 }
}
def normalize( s ){
  s = s.replace(' ','')
  #(l,r) = s.split('=')
  ld = gen_expr ( l )
  rd = gen_expr ( r )
  // merge 
  u = ld.keys | rd.keys // union 
  poly = list ( u ) -> { 
     k = $.item ; coeff = 0.0 
     if ( k @ ld ){ coeff += sum ( ld[k] )->{  $.item[0] }  }
     if ( k @ rd ){ coeff -= sum ( rd[k] )->{  $.item[0] }  }
     [ k , coeff ]
  }
  sortd ( poly ) :: { #(l,r) = $.item ; l.0 < r.0 }
  str( poly , '' ) -> { #(k,c) = $.item  ; str( '%+f%s' , c , k ) } 
}
x = normalize ( 'x^2 + 3.5xy + y = y^2 - xy + y + 10' )
println ( x )

- NoOne October 19, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what language is it? php or python or ruby?

- mxu2008 February 19, 2017 | 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