My coding style is influenced by seminal books such as: Clean Code and Effective Java. I highly value code clarity and testability. When it makes sense, I liberally use well-known libraries and frameworks such as: Spring, Google Guava, RestEasy, Apache Commons, etc... At other times, again when it makes sense, I might eschew those very same libraries/frameworks if their costs outweigh their benefits.
A trio of, perhaps unusual coding quirks of mine:
-
I avoid creating unnecessary variables, i.e. those which would only be used once;
-
When practical, I avoid inline comments, in favor of expressive variable names and code statements; and
-
I avoid unnecessary line breaks, as I expressly use them, or not, to convey how related contiguous code lines are to one another.
Write a function to reverse a string.
My answers are located in
com.cybercoders.interview.Exercise1.java and
com.cybercoders.interview.Exercise1Test.java
Print out the grade school multiplication table up to 12 x 12.
My answers are located in
com.cybercoders.interview.Exercise2.java and
com.cybercoders.interview.Exercise2Test.java
Define a tree where every node can have many children. Write a method to print out the elements of the tree breadth first backwards starting with the bottom tier nodes and working up the tree.
My answers are located in
com.cybercoders.interview.Exercise3.java and
com.cybercoders.interview.TreeNodeTest.java
Model the Animal kingdom as a class system, for use in a Virtual Zoo program.
Before delving into my design approach to the class system for this Zoo program, I feel it is important to point out that my design approach would have been vastly different, and probably a lot simpler, had I been asked to design the same class system for a brick 'n mortar zoo, probably interested in recording animal care. In that hypothetical situation, I would not have to worry about modeling each physical action that animals are allowed to take, like moving on land, air, or sea.
For fear of getting too lost in the details when answering a design question like this, I'll stick to these high-level design objectives:
- Clearly identify and model domain objects and any meaningful variations, e.g. Mammal, Reptile, Fish, Amphibian, Bird, etc...
- Clearly identify application-level objects and not conflate them and their responsibilities with domain objects (I often see this problem).
- Avoid an explosion of classes, and keep business logic simple, by strategically using interfaces to model animal behaviors that don't have to be shared all across the board, as would be the case if class inheritance were heavily relied on for the same. I guess what I'm trying to express here is the principle of "coding to interfaces".
- Start with the simplest design possible, then iterate into more complex designs, only as necessary to meet some business or system requirement.
Given the above design objectives and my own design sensibilities, I expect that my design would include the minimum set of perhaps:
- These classes: Mammal, Reptile, Fish, Amphibian, Bird, etc... each of these classes may contian a
subTypeattribute (probably of typeenum). - And these interfaces: ColdBlooded, HotBlooded, Flier, Swimmer, Domesticatable, Tamable, Untamable, etc...
Examine the given Java code and answer the following questions:
- What does it do? This is what the method name
funcdoes:- Accepts an array of strings and an integer as the input parameters
aandbottom, respectively - Attempts to convert each
Stringinto aDoubleby reading them into a LinkedList namedconverted - Dumps the items in
converted, except for the last one, into an array of primitive doubles namedb - If input array parameter
ahad a length of one:- Attempts to Parse the
Stringinto aDoubleand return it
- Attempts to Parse the
- Otherwise:
- Parse the last string in array
ainto a variable of typeDouble - Fails to evaluate the statements in the subsequent
ifstatement, because of a bug resulting from reading outside the bounds of arrayb - If not for the above-mentioned bug and had the above-mentioned
ifstatement evaluated totrue, the intention of the subsequent code was to:- Replace the last value in array
bwith the last value in input arraya - Marshal all the items in array
binto an array of strings namedargs, while converting them to strings - Finally, the
funcmethod recursively calls itself, passing inargsandbottomas input parameters - If we were to pretend that the previously-mentioned bug did not exist, the recursive calls would, indeed, stop as the length of the
argsarray is reduced until it only contains one element, thus meeting the recursion's end case.
- Replace the last value in array
- Parse the last string in array
- Accepts an array of strings and an integer as the input parameters
- Does it have any bugs?
- Yes, there is that bug I pointed out in my answer to the previous question.
Another bug is that of not handling
NumberFormatExceptions, if any of the input strings could not be parsed into aDouble.
- Yes, there is that bug I pointed out in my answer to the previous question.
Another bug is that of not handling
- Describe the effectiveness of its use of Java classes to accomplish its goal?
- I don't like how the values in the input
Stringarray are naively converted intoDoubleswithout handling the uncheckedNumberFormatException. - The input parameter
bottomis never used for anything. - Brackets around
forloops are sometimes used, sometimes not, this is sloppy styling.
- I don't like how the values in the input
- Describe the effectiveness of its algorithm.
- The algorithm is effective, for whatever the code's goal is (and bugs not-withstanding), but the recursion is not necessary and can be easily replaced with an iteration-based equivalent.
The code snippet I used to debug into what this sample code was trying to do can be found at:
com.cybercoders.interview.Exercise5.java