RSS

Tag Archives: tech-challenge

Algorithm for Shortest Path

Today’s tech challenge comes from Garry Trinder’s The Old New Thing.

Consider a two-dimensional board, tall and narrow. Into the board are nailed a number of horizontal obstacles. Place a water faucet at the top of the board and turn it on. The water will dribble down, and when it hits an obstacle, some of the water will go left and some will go right. The goal is to find the shortest path to the ground from a given starting position, counting both horizontal and vertical distance traveled.

The illustration:

algorithm-for-shortest-path

Head over to his blog for solution in C# and JavaScript.

Advertisements
 
Leave a comment

Posted by on June 8, 2015 in General

 

Tags:

int.SubString() ?

How would you write a Substring function for an Integer?

 
Leave a comment

Posted by on April 16, 2015 in General

 

Tags: , ,

Determine Hierarchy Name Based on Inconsistent Level

Below are sets of inconsistent arrays that represents level in hierarchy:

  • [0, 1, 2]
  • [0, 1]
  • [0]

The order of hierarchy is (from the top to the bottom):

  • Director
  • Manager
  • Employee

The highest number in the array represents the bottom level in the hierarchy. The lowest number in the array represents higher level in the hierarchy.

So, given [0, 1, 2]: 0 means Director, 1 means Manager and 2 means Employee. And given [0, 1]: 0 means Manager and 1 means Employee.

What’s the most efficient way to assign LevelName property in the following data set. The answer doesn’t have to use specific programming language.

[{
    Fullname: "Bob",
    ReportTo: "",
    Level: "0",
    LevelName: ""
},{
    Fullname: "John",
    ReportTo: "Bob",
    Level: "1",
    LevelName: ""
},{
    Fullname: "Sally",
    ReportTo: "John",
    Level: "2",
    LevelName: ""
},{
    Fullname: "David",
    ReportTo: "John",
    Level: "2",
    LevelName: ""
},{
    Fullname: "Rachel",
    ReportTo: "Bob",
    Level: "1",
    LevelName: ""
},{
    Fullname: "Peter",
    ReportTo: "Rachel",
    Level: "2",
    LevelName: ""
}]
 
Leave a comment

Posted by on January 28, 2015 in General

 

Tags: , , ,

What is the Best and Fastest Way to Flatten Data?

Given the following data set, what’s the best and fastest way to flatten them? This can be achieved with LINQ, SQL, C#, or whatever else you prefer.

001 12/5/2013 113 143 11143 44 673
001 12/6/2013 113 143 11143 44 673
001 12/7/2013 113 143 11143 44 673
001 12/8/2013 113 143 11143 44 673
001 12/9/2013 113 143 11143 44 673
001 12/10/2013 113 143 11143 44 673
002 12/5/2013 113 143 11143 44 673
002 12/6/2013 113 143 11143 44 673
002 12/7/2013 113 143 11143 44 673
002 12/8/2013 113 143 11143 44 673
002 12/9/2013 113 143 11143 44 673
002 12/10/2013 113 143 11143 44 673

Desired result:

001 12/5/2013 113 143 11143 44 673 12/6/2013 113 143 11143 44 673 12/7/2013 113 143 11143 44 673 12/8/2013 113 143 11143 44 673 12/9/2013 113 143 11143 44 673 12/10/2013 113 143 11143 44 673
002 12/5/2013 113 143 11143 44 673 12/6/2013 113 143 11143 44 673 12/7/2013 113 143 11143 44 673 12/8/2013 113 143 11143 44 673 12/9/2013 113 143 11143 44 673 12/10/2013 113 143 11143 44 673
 
Leave a comment

Posted by on December 3, 2014 in General

 

Tags: , , ,

Compare Two URL Strings

URL String Compare

What is the best approach to compare two URLs and determine if they are the same or not?

For example:
https://stack247.wordpress.com and stack247.wordpress.com are the same URL.
https://stack247.wordpress.com and stack247.wordpress.com are the same URL.
https://stack247.wordpress.com/tag/interview-question/ and stack247.wordpress.com/tag are NOT the same URL.

 
Leave a comment

Posted by on July 31, 2013 in General

 

Tags: , , ,

Android Keyboard Auto Suggest Design

This time tech challenge is about designing Android keyboard auto suggest.

Android keyboard auto suggest is a feature to suggest words that match what you have typed even if there’s typo.

android-keyboard-auto-suggest-design

As the above screenshot shows, Android system suggests ‘Thus’ and ‘This’ words when I type ‘Rgua’. Although ‘Rgua’ is a typo, it knows what I intend to type (which is ‘This’) because the letters’ position in ‘Rgua’ are as if I am typing ‘This’.

There is another trick to this feature. That is to suggest correct words in absence of space. For example, if I type ‘mytournament’ or ‘mybtournament’ or ‘myctournament’, the system will suggest ‘my tournament’. Notice it recognizes ‘b’ and ‘c’ as a space.

If you are to design / architect this, what’s your approach?

 
Leave a comment

Posted by on May 15, 2013 in General

 

Tags: ,

String Join Array Without Converting to List

I have the following code:


var fullName = "John Doe Smith";

var names = fullName.Split(' ');

var firstName = names[0];

//var lastName = ...

How can we populate lastName variable without converting names[] to List<T>?

 
Leave a comment

Posted by on February 7, 2013 in General

 

Tags: , ,

 
%d bloggers like this: