Posts Tagged ‘Algorithms’
Triangle Identification – C++ solution and test code
Here is another interview coding question that I have heard of. You have to write a function to identify the triangle type. You have to return a number between 1 or 4 to identify either a valid triangle type or an error. The solution is very simple requiring only one condensed line of code to identify each triangle type. Two lines of code [...]
Reverse the words in a sentence – C++ solution and test code
Here is a solution to the standard interview questions of reversing the letters in the words of a sentence. It is a more complex version of the even more common "reverse a string" question. I also include my test code. There are two main parts to this. The first identifies where the word boundaries are. The second reverses the letters between [...]
Picking the 5th from last element in a singly linked list
Here's another of those fun coding interview puzzles. The kind that companies like Microsoft or Google might ask you. Given a singly linked list, select the 5th from last element. I'm writing the solution in C++ but the solution in Java would be almost identical. I'm also putting up my test code. The restrictions are - you can only make one [...]
Reverse a linked list – C++ source code
Reversing a linked list is a simple programming problem, which is often a interview question. In this case I'm referring to a singly linked list. I'll provide the C++ solution and the C++ test code. One of the easiest ways to reverse the list is to create a new head pointer, iterate through the list removing each item in turn and then inserting [...]
Dijkstra’s Algorithm code in C++
Dijkstra's algorithm is a famous algorithm that calculates the routes and distances from a start node to all other nodes in a connected graph where all the distances are positive. Here is a version which I wrote in C++. My aim here was to try to create a version that was easy to read, rather than the most efficient version. This version is [...]
Select 100 random values from a stream / linked list
Here's an interesting puzzle / interview question that I heard recently. Problem You have a either a linked list or a stream containing int values. You don't know how large the stream / linked list except that it contains 100 or more values. You need to write a method that selects 100 random values from the stream. You have to write the [...]
QuickSort Implementation Code in C++
I was having a look at the QuickSort algorithm (as you do for fun) and was trying to find a tidy looking implementation in C++. I found a lot of implementations out there but most of them looked far more complicated than I thought they should have been. The QuickSort algorithm as printed in the book looks relatively simple. I wanted a C++ [...]









