Image for post
Image for post
Photo by Fletcher Pride on Unsplash

Do You Know How to Solve These Programming Problems?

Can you wrap your head around all of them?

Daan
Daan
Dec 20, 2019 · 5 min read

Programming is about solving problems. In this piece, I’ve listed six programming problems from several sites that contain programming problems. The problems in this listed are sorted based on how difficult they are to solve — with number one on this list being the easiest, and number six being the most difficult to solve. Can you wrap your head around all of them?

I provided the solutions to these programming problems, coded in PHP, at the bottom of this article. You can choose a programming language of your choice to solve these problems.

Good luck solving these problems, and happy coding!

Challenges

Let’s start with a relatively easy problem provided by HackerRank. This challenge is categorized as a warmup.

Image for post
Image for post

A challenge that’s considered easy, provided by LeetCode.

Image for post
Image for post

This problem is provided by Project Euler and is considered one of the more easy problems. It’s currently solved by more than 455,000 people.

This is the problem:

Image for post
Image for post

Another challenge from Project Euler. This one is a little bit harder than the previous problem. It’s solved by around 100,000 people.

Image for post
Image for post

If you’ve made it up to this point: Congratulations! It’s time to start the first hard challenge. This challenge is provided by Coderbyte.

Image for post
Image for post

Definitely the toughest challenge so far, this challenge is provided by LeetCode. Although it’s considered to be of medium difficulty, I found this one harder to solve than the Kaprekars Constant. This one requires you to know how linked lists work.

But let’s not go into too much detail — here’s the challenge:

Image for post
Image for post

Solutions

A really great problem to get you started with a solution that’s really straightforward.

<?phpfunction getFractionals($numbers) {
$length = count($numbers);
$results = [
'positive' => 0,
'negative' => 0,
'zero' => 0,
];

for ($i = 0; $i < $length; $i++) {
if ($numbers[$i] < 0) {
$results['negative'] += 1;
} else if ($numbers[$i] > 0) {
$results['positive'] += 1;
} else {
$results['zero'] += 1;
}
}

return [
$results['positive'] / $length,
$results['negative'] / $length,
$results['zero'] / $length
];
}
print_r(getFractionals([1, 1, 0, -1, -1])); // [0.4, 0.4, 0.2]
print_r(getFractionals([-4, 3, -9, 0, 4, 1])); // [0.5, 0.3333, 0.16667]

Although this one’s a little bit harder than the first problem, you shouldn’t have too much trouble solving this one. I used a simple brute-force approach.

<?phpfunction twoSum($numbers, $target) {
for ($i = 0; $i < count($numbers); $i++) {
for ($j = $i + 1; $j < count($numbers); $j++) {
if ($numbers[$j] + $numbers[$i] === $target) {
return [$i, $j];
}
}
}
}
print_r(twoSum([2, 7, 11, 15], 9)); // [0, 1]
print_r(twoSum([2, 7, 11, 15], 17)); // [0, 3]

The solution I came up with has the advantage it can be used to find the biggest palindrome that’s the product of any two x-digit numbers.

I’ve also added stop conditions to avoid unnecessary extra looping.

<?phpfunction isPalindrome($number) {
return (string) $number === strrev((string) $number);
}
function getBiggestPalindrome($digits) {
$start = pow(10, $digits) - 1;
$max = 0;
for ($i = $start; $i > 0; $i--) {
if ($i * $start <= $max) {
break;
}
for ($j = $start; $j > 0; $j--) {
$product = $i * $j;
if ($product < $max) {
break;
}
if ($product > $max && isPalindrome($product)) {
$max = $product;
}
}
}
return $max;
}
echo getBiggestPalindrome(2); // 9009
echo getBiggestPalindrome(3); // 906609, which is 993 * 913

I solved the distinct powers problem by going the brute-force route.

Add each result to the array, and then remove duplicates from the array. The last step is to sort the array.

<?phpfunction distinctPowers($min, $max) {
$numbers = [];

for ($i = $min; $i <= $max; $i++) {
for ($j = $min; $j <= $max; $j++) {
$numbers[] = pow($i, $j);
}
}

$unique_numbers = array_unique($numbers);
sort($unique_numbers);

return $unique_numbers;
}
echo print_r(distinctPowers(2, 5), 1); // [4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125]
echo print_r(count(distinctPowers(2, 100)), 1); // 9183 distinct terms

The Kaprekars Constant problem is a little bit harder to solve. It’s the first problem in this list that requires recursion to solve the problem.

function KaprekarsConstant($number, $numberOfIterations = 1) {
$number = (string) $number;

if (strlen($number) < 4) {
for ($i = strlen($number); $i < 4; $i++) {
$number .= '0';
}
}

$asc = str_split($number);
$desc = $asc;

rsort($desc);
sort($asc);

$asc_number = (int) implode($asc, '');
$desc_number = (int) implode($desc, '');
$difference = abs($asc_number - $desc_number);

if ($difference !== 6174) {
return KaprekarsConstant($difference, $numberOfIterations + 1);
}

return $numberOfIterations;
}
echo KaprekarsConstant(2111); // 5
echo KaprekarsConstant(9831); // 7
Image for post
Image for post
A screenshot of passing all test cases

This one took me a while to figure out. The trick in my solution is to pass variables by reference instead of by value. Still, this one might take some time to get your head around.

function swapPairs($head) {
$current = &$head;

while (!is_null($current->next)) {
$nextValue = $current->next->val;

$temp = &$current;
$temp->next->val = $temp->val;
$temp->val = $nextValue;

$current = &$current->next->next;
}

return $head;
}
Image for post
Image for post

Want to Solve More Programming Problems Yourself?

If you enjoyed solving these problems, go to one of the websites that I mentioned in this piece. Most websites provide plenty of free challenges that you can try to solve.

Better Programming

Advice for programmers.

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Daan

Written by

Daan

Backend developer from The Netherlands. Crypto enthusiast.

Better Programming

Advice for programmers.

Daan

Written by

Daan

Backend developer from The Netherlands. Crypto enthusiast.

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store