Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 35665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:16:18+00:00 2026-05-10T14:16:18+00:00

This morning, I was reading Steve Yegge’s: When Polymorphism Fails , when I came

  • 0

This morning, I was reading Steve Yegge’s: When Polymorphism Fails, when I came across a question that a co-worker of his used to ask potential employees when they came for their interview at Amazon.

As an example of polymorphism in action, let’s look at the classic ‘eval’ interview question, which (as far as I know) was brought to Amazon by Ron Braunstein. The question is quite a rich one, as it manages to probe a wide variety of important skills: OOP design, recursion, binary trees, polymorphism and runtime typing, general coding skills, and (if you want to make it extra hard) parsing theory.

At some point, the candidate hopefully realizes that you can represent an arithmetic expression as a binary tree, assuming you’re only using binary operators such as ‘+’, ‘-‘, ‘*’, ‘/’. The leaf nodes are all numbers, and the internal nodes are all operators. Evaluating the expression means walking the tree. If the candidate doesn’t realize this, you can gently lead them to it, or if necessary, just tell them.

Even if you tell them, it’s still an interesting problem.

The first half of the question, which some people (whose names I will protect to my dying breath, but their initials are Willie Lewis) feel is a Job Requirement If You Want To Call Yourself A Developer And Work At Amazon, is actually kinda hard. The question is: how do you go from an arithmetic expression (e.g. in a string) such as ‘2 + (2)’ to an expression tree. We may have an ADJ challenge on this question at some point.

The second half is: let’s say this is a 2-person project, and your partner, who we’ll call ‘Willie’, is responsible for transforming the string expression into a tree. You get the easy part: you need to decide what classes Willie is to construct the tree with. You can do it in any language, but make sure you pick one, or Willie will hand you assembly language. If he’s feeling ornery, it will be for a processor that is no longer manufactured in production.

You’d be amazed at how many candidates boff this one.

I won’t give away the answer, but a Standard Bad Solution involves the use of a switch or case statment (or just good old-fashioned cascaded-ifs). A Slightly Better Solution involves using a table of function pointers, and the Probably Best Solution involves using polymorphism. I encourage you to work through it sometime. Fun stuff!

So, let’s try to tackle the problem all three ways. How do you go from an arithmetic expression (e.g. in a string) such as ‘2 + (2)’ to an expression tree using cascaded-if’s, a table of function pointers, and/or polymorphism?

Feel free to tackle one, two, or all three.

[update: title modified to better match what most of the answers have been.]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:16:18+00:00Added an answer on May 10, 2026 at 2:16 pm

    Polymorphic Tree Walking, Python version

    #!/usr/bin/python  class Node:     '''base class, you should not process one of these'''     def process(self):         raise('you should not be processing a node')  class BinaryNode(Node):     '''base class for binary nodes'''     def __init__(self, _left, _right):         self.left = _left         self.right = _right     def process(self):         raise('you should not be processing a binarynode')  class Plus(BinaryNode):     def process(self):         return self.left.process() + self.right.process()  class Minus(BinaryNode):     def process(self):         return self.left.process() - self.right.process()  class Mul(BinaryNode):     def process(self):         return self.left.process() * self.right.process()  class Div(BinaryNode):     def process(self):         return self.left.process() / self.right.process()  class Num(Node):     def __init__(self, _value):         self.value = _value     def process(self):         return self.value  def demo(n):     print n.process()  demo(Num(2))                                       # 2 demo(Plus(Num(2),Num(5)))                          # 2 + 3 demo(Plus(Mul(Num(2),Num(3)),Div(Num(10),Num(5)))) # (2 * 3) + (10 / 2) 

    The tests are just building up the binary trees by using constructors.

    program structure:

    abstract base class: Node

    • all Nodes inherit from this class

    abstract base class: BinaryNode

    • all binary operators inherit from this class
    • process method does the work of evaluting the expression and returning the result

    binary operator classes: Plus,Minus,Mul,Div

    • two child nodes, one each for left side and right side subexpressions

    number class: Num

    • holds a leaf-node numeric value, e.g. 17 or 42
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer I would use something like Server.ResolveClientUrl('~/common/css/global.css') This will get a… May 11, 2026 at 12:40 pm
  • added an answer I would suggest using nmap's ping-scan flag, $ nmap -sn… May 11, 2026 at 12:40 pm
  • added an answer This is not a good idea, there must be a… May 11, 2026 at 12:40 pm

Related Questions

This morning, I was reading Steve Yegge's: When Polymorphism Fails , when I came
I was messing around with RhinoMocks this morning and couldn't run my tests because
This morning, I tried to commit a revision to Subversion and found that all
This morning, when I tried to add a new ASPX page to my project,
This morning I ran into an issue with returning back a text string as
Good morning, afternoon, evening or night (depending on your timezone). This is just a
I am out of ideas as to why my app has suddenly stopped working

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.