Grokking Multithreading and Concurrency for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Problem 11: Palindrome Multithreaded Investigator
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Overview

The challenge at hand is to evaluate a sequence of numbers and categorize them into palindromes and non-palindromes. A palindrome is a number (or a word) that reads the same backward as forward (e.g., 121, 12321).

In this problem, the task of categorization is done by two separate threads:

  • Thread A: Responsible for detecting and outputting palindromes.
  • Thread B: Responsible for detecting and outputting non-palindromes.

The challenge is magnified by the constraint that the numbers should be output in sequence, regardless of their categorization. This implies the need for synchronization between the threads to maintain the sequence of the input while processing in parallel.

Concurrency Introduction

Concurrency introduces the concept of executing multiple sequences of instructions in overlapping time periods. In this problem, we're using two threads to evaluate a series of numbers in parallel, which will require synchronization to ensure that the output is consistent with the sequence of the input.

Threading can optimize performance, especially when tasks can be run independently. However, when threads need to access shared data (like our list of numbers), synchronization becomes essential. Synchronization ensures that threads don't interfere with each other and the results remain consistent.

Pseudocode

Initialize an array of numbers, currentIndex to 0, and other synchronization primitives Function IsPalindrome(number): Convert number to string Return true if string is the same when reversed, else false ThreadFunction DetectPalindromes: While currentIndex is less than array length: Acquire lock: If number at currentIndex is a palindrome: Output number as palindrome Increment currentIndex Signal non-palindrome thread and wait for the palindrome thread ThreadFunction DetectNonPalindromes: While currentIndex is less than array length: Acquire lock: If number at currentIndex is not a palindrome: Output number as non-palindrome Increment currentIndex Signal palindrome thread and wait for the non-palindrome thread Main: Start both threads Wait for both threads to finish

This pseudocode gives an abstract view of how the problem will be approached. The "lock" ensures that at any given time, only one thread evaluates and outputs a number. The "signals" and "waits" are synchronization primitives to control the order of execution and ensure numbers are processed in sequence.

Algorithm Walkthrough

Let's walk through a simple example with an array of numbers [123, 121, 3223, 4554, 1234].

Initialization:

  • We have an array numbers = [123, 121, 3223, 4554, 1234], and a shared variable currentIndex = 0.
  • We also need two signaling conditions or semaphores, one for palindromes and another for non-palindromes, to manage turn-taking between threads.

Thread A (Palindrome Detector):

  • Thread A starts and checks the first number, 123.
  • It determines if the number is a palindrome (which it is not).
  • Since it's not a palindrome, Thread A does not output anything but must wait for Thread B to check this number and potentially print it.

Thread B (Non-Palindrome Detector):

  • Thread B looks at the current index, sees the number 123, and confirms it's not a palindrome.
  • Thread B prints "123: Not a palindrome" and increments currentIndex to 1.
  • It then signals Thread A (if we're using condition variables, for instance) to check the next number and waits for its turn to come back around.

Thread A:

  • Now currentIndex is 1, and the number is 121.
  • Thread A checks 121, determines it is a palindrome, prints "121: Palindrome", increments currentIndex to 2, and signals Thread B.

Thread B:

  • Waits until it's its turn to check number 3223 at currentIndex 2.
  • Finds that 3223 is a palindrome, does not print, increments currentIndex, and signals Thread A.

Thread A:

  • Gets the signal, checks the next number (which is now 4554), finds it's a palindrome, prints "4554: Palindrome", increments currentIndex to 4, and signals Thread B.

Thread B:

  • Receives the signal, sees the number 1234 at currentIndex 4, determines it's not a palindrome, prints "1234: Not a palindrome", and increments currentIndex to 5.

Since there are no more numbers to check (currentIndex is now equal to the length of the array), both threads can terminate.

In this setup, only one thread operates on the array at any given time, controlled by the signaling mechanism. The signaling ensures that the two threads take turns in a controlled manner, and the currentIndex variable makes sure they are always looking at the correct position in the array.

mediaLink

1 of 6

Python3
Python3

. . . .

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible