coding problems

Mergesort Investigation 4 - reuse linked lists

Mergesort Investigation 4 - reuse linked lists

Bruce Ediger

After I wrote a recursive merge sort, I reviewed it to ensure it wasn’t doing extra work. From that perspective, I noticed that for each of the 10 list sorts my benchmark program made for a given length linked list, it created a new linked list.

I thought that I could make the overall benchmark run faster by not creating a new, very large, linked list for every run, but rather re-using the existing list. I hoped to have the benchmark program use less time between the ten measured sort algorithm executions, not make the sorting faster. I surprised myself by getting the opposite effect.

Mergesort Investigation 3 - recursive implementation

Mergesort Investigation 3 - recursive implementation

Bruce Ediger

I wrote a traditional, top-down, recursive merge sort. The idea was to see if a different implementation exhibited the same dramatic, abrupt slowdowns. If the different implementation showed the same jumps in timings, the problem is with the operating system, the language run time, or the memory hardware. I know I said I’d ruled out the underlying operating system previously, but I was still suspicious.

Programming Puzzle Gone Wrong

Programming Puzzle Gone Wrong

Bruce Ediger

A programming interview question from the Daily Coding Problem email list. Here’s a non-hand-wavy explanation of a way to solve this problem.

Daily Coding Problem: Problem #736 [Easy]

Given a complete binary tree, count the number of nodes in faster than O(n) time. Recall that a complete binary tree has every level filled except the last, and the nodes in the last level are filled starting from the left. “Complete” means: every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes at the last level h.

Count number of heaps coding problem

Count number of heaps coding problem

Bruce Ediger

Another programming interview question from the Daily Coding Problem email list. I received it as #1608.

Daily Coding Problem: Problem #1608 [Medium]

This problem was asked by Microsoft.

Write a program to determine how many distinct ways there are to create a max heap from a list of N given integers.

For example, if N = 3, and our integers are [1, 2, 3], there are two ways, shown below.

  3      3
 / \    / \
1   2  2   1

Repo for my code.

Character flipping programming interview problem

Character flipping programming interview problem

Bruce Ediger

Programming interview question from the Daily Coding Problem email list. I have it as #1602, but other folks have it as #331 from 2020. I guess I missed it back then.

Daily Coding Problem: Problem #1602 [Medium]

This problem was asked by LinkedIn.

You are given a string consisting of the letters x and y, such as xyxxxyxyy. In addition, you have an operation called flip, which changes a single x to y or vice versa.

Determine how many times you would need to apply this operation to ensure that all x’s come before all y’s. In the preceding example, it suffices to flip the second and sixth characters, so you should return 2.


Repo for code.