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

Related Questions

I was doing some reading on REST this morning and I came across the
I started reading on OAuth this morning; need suggestions(links et al.) that will help
I have been reading the developer blog on Facebook this morning and stumbled across
This morning we found an old chunk of code that was causing a library
So this morning I posted a confused question about assembly and I received some
I found out this morning that proc.new works in a class initialize method, but
I was reading this morning the book The Pragmatic Programmer Chapter 3 on Basic
This morning, I was reading a news article on Apple (either Snow Leopard or
I've spent all morning messing with this now and reading on here, but have
This morning, I was afraid when I discovered that it is possible to display

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.