Code Monkey home page Code Monkey logo

codeinterview002's Introduction

Coding questions

I used Visual Studio 2012 and C# to implement following questions. For unit tests I used nUnit framework (to launch nUnit tests in Visual Studio you need to have nUnit test adapter or you can use ReSharper to launch them).

Question 1

Given a string, write a routine that converts the string to a long, without using the built in functions that would do this. Describe what (if any) limitations the code has. For example:

long stringToLong(String s)
{
    /* code goes here to convert a string to a long */
}

void test()
{ 
    long i = stringToLong("123");
    if (i == 123)
        // success
    else
        // failure
}

Answer

Function stringToLong is implemented in file Parser.cs. Unit tests are implemented in ParserSuites.cs. Current implementation has several limitations:

  1. Function cannot parse long values represented in HEX format, like 0x123 (unit test StringToLong_HexValue_ThrowsFormatException).
  2. Function cannot parse long values in strings, which contain group separators, like 1,000,000 (unit test StringToLong_CultureSpecificInputValueWithGroupSeparators_ThrowsFormatException(.

Question 2

Implement insert and delete in a tri-nary tree. A tri-nary tree is much like a binary tree but with three child nodes for each parent instead of two -- with the left node being values less than the parent, the right node values greater than the parent, and the middle nodes values equal to the parent.

For example, suppose I added the following nodes to the tree in this order: 5, 4, 9, 5, 7, 2, 2.

The resulting tree would look like this:

     5
   / | \
  4  5  9
 /     /
2    7
|
2

Answer

Tri-nary tree structure is implemented in file TrinaryTree.cs. Current implementation allows to use any value types as a structure values. Because all values are value types we can use one trick to store middle-node. On middle node you can store only values which are equal to parent node. That means that by middle-node direction we can have only values which are equal to parent node, so we do not need to store real nodes, we can just count what is the depth of middle nodes.

To store any C# types in current structure we will need to change couple places: a) add validations for null values, b) change how we store middle node, instead of simple counter we can use Stack for that.

Unit tests for TrinaryTree are implemented in TrinaryTreeSuites.cs. This class contains only two methods, but in fact it contains 13 unit tests. One of unit tests Remove_TestCase expects test cases, which we provide with source RemoveTestCaseSource (see documentation for TestCaseSourceAttribute)

codeinterview002's People

Contributors

outcoldman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.