Manafall Daily Quest

LinkedList Cycle Detector

Function Name: hasCycle

Description

Write a function `hasCycle` in a language of your choice that takes a linked list as an input and returns a boolean indicating whether the linked list contains a cycle.

A linked list is said to contain a cycle if any node is visited more than once while traversing the list.

To solve this problem, you should detect if the linked list has a cycle without using extra space (e.g., an additional data structure like a hash table).

Assume that the linked list is a singly linked list provided with the next pointer.

Your function should be able to return false if the input is an empty list or a single-node list without a cycle, and true if a cycle is detected.

Requirements

  • The function `hasCycle` should take a single parameter that represents the head of the linked list.
  • The function should return a boolean value.
  • Do not use external libraries or additional data structures.
  • Minimize the space complexity of the function; strive for O(1) space.
  • Consider edge cases like an empty list and lists with only one node.
  • You must use Floyd's Cycle-Finding Algorithm (also known as the tortoise and hare algorithm).

Examples

If the linked list has the following nodes where the last node points back to the second node: 1 -> 2 -> 3 -> 4 -> 2 (cycle), `hasCycle(head)` should return true.If the linked list has the following nodes with no cycle: 1 -> 2 -> 3 -> 4, `hasCycle(head)` should return false.For an empty list, `hasCycle(head)` should return false.For a single-node list, `hasCycle(head)` should also return false.

Links

https://en.wikipedia.org/wiki/Cycle_detectionhttps://en.wikipedia.org/wiki/Floyd%27s_cycle-finding_algorithm

TicTacToe Validator

Mon Oct 21 2024

Write a function that takes a two-dimensional array (3x3) representing a completed Tic Tac Toe game and determines if the board is in a valid state. A Tic Tac Toe board is valid if:- There are either an equal number of 'X's and 'O's, or one more 'X' than 'O's (since 'X' always goes first).- There cannot be two winners (both 'X' and 'O' cannot have a winning line).- A winning line is considered as three 'X's or 'O's in a row, column, or diagonally.

Prev Quest

Robot Room Cleaner

Wed Oct 23 2024

Imagine a robot sitting on the upper left corner of a grid with rows and columns. The grid is filled with dirt (represented by a '1'), and clean tiles are represented by a '0'. The robot can move up, down, left, or right, and it can clean the tile it's on. The function 'cleanRoom' should simulate the cleaning process. It takes a 2D array representing the room's condition, a start position for the robot, and returns the number of tiles cleaned by the robot. It cleans each dirty tile only once, regardless of the path taken.

Next Quest