Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 72 additions & 1 deletion CodingChallenge.FamilyTree/Solution.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,83 @@
using System;
using System.Collections.Generic;

namespace CodingChallenge.FamilyTree
{
public class Solution
{
public string GetBirthMonth(Person person, string descendantName)
{
throw new NotImplementedException();
if (person == null || string.IsNullOrEmpty(descendantName))
{
return string.Empty;
}

var queue = new Queue<Person>();
queue.Enqueue(person);

while (queue.Count > 0)
{
var currentPerson = queue.Dequeue();

// Check the current person
if (currentPerson.Name == descendantName)
{
return currentPerson.Birthday.ToString("MMMM");
}

// Enqueue all descendants of the current person to search in the next level
foreach (var descendant in currentPerson.Descendants)
{
queue.Enqueue(descendant);
}
}

// If we've searched the entire tree and haven't found the person
return string.Empty;
}

//Memoization version for very large datasets and in high-performance scenarios
/*
private Dictionary<string, string> _cache = new Dictionary<string, string>();

public string GetBirthMonth(Person person, string descendantName)
{
if (person == null || string.IsNullOrEmpty(descendantName))
{
return string.Empty;
}

// Check cache first
if (_cache.TryGetValue(descendantName, out var cachedResult))
{
return cachedResult;
}

var queue = new Queue<Person>();
queue.Enqueue(person);

while (queue.Count > 0)
{
var currentPerson = queue.Dequeue();

// Check for reference equality and case-sensitive name match
if (ReferenceEquals(currentPerson.Name, descendantName) ||
currentPerson.Name.Equals(descendantName, StringComparison.Ordinal))
{
var birthMonth = currentPerson.Birthday.ToString("MMMM");
_cache[descendantName] = birthMonth; // Cache the result
return birthMonth;
}

foreach (var descendant in currentPerson.Descendants)
{
queue.Enqueue(descendant);
}
}

_cache[descendantName] = string.Empty; // Cache the negative result
return string.Empty;
}
*/
}
}
12 changes: 12 additions & 0 deletions CodingChallenge.FamilyTree/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,15 @@ Once that is complete, augment the method to handle Names that do not exist in t

Comments are appreciated to explain thought process.
Feel free to add/remove anything from this Solution that will help you in solving the problem. Comments are appreciated to explain thought process.

## Solution Overview
The solution implements a breadth-first search (BFS) algorithm to traverse the family tree efficiently.
This approach ensures that all nodes (people) at the current depth (generation) are explored before moving on to the next level.

## Features
- Robust Error Handling: Checks for null or empty inputs to prevent unnecessary processing.
- Breadth-First Search: Utilizes BFS to ensure minimum traversal and early termination upon finding the target individual.

## Memoization Version
- Optimized String Comparison: Employs StringComparison.Ordinal for faster string comparisons.
- Caching: Implements a simple cache to store and retrieve previously searched results quickly.
41 changes: 40 additions & 1 deletion CodingChallenge.PirateSpeak/Solution.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,46 @@ public class Solution
{
public string[] GetPossibleWords(string jumble, string[] dictionary)
{
throw new NotImplementedException();
if (string.IsNullOrWhiteSpace(jumble) || dictionary == null || dictionary.Length == 0)
{
return new string[0];
}

var jumbledMessageLetterCounts = GetLetterCounts(jumble.ToLower());

var matchingWords = dictionary
.Where(word => LetterCountsMatch(jumbledMessageLetterCounts, GetLetterCounts(word.ToLower())))
.ToArray();

return matchingWords;
}

private int[] GetLetterCounts(string word)
{
var letterCounts = new int[26];

foreach (var letter in word)
{
if (char.IsLetter(letter))
{
letterCounts[letter - 'a']++;
}
}

return letterCounts;
}

private bool LetterCountsMatch(int[] counts1, int[] counts2)
{
for (var i = 0; i < 26; i++)
{
if (counts1[i] != counts2[i])
{
return false;
}
}

return true;
}
}
}
8 changes: 7 additions & 1 deletion CodingChallenge.PirateSpeak/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ Write a function that will accept a jumble of letters as well as a dictionary, a
For example:

GetPossibleWords ("ortsp", ["sport", "parrot", "ports", "matey"])
Should return ["sport", "ports"].
Should return ["sport", "ports"].

##Approach
- Check for edge cases where the jumble is empty or contains no letters, or if the dictionary is empty.
- Convert both the jumble and dictionary words to lowercase for case-insensitive comparison.
- Count the occurrences of each letter in both the jumble and dictionary words.
- Compare the letter counts to determine if a dictionary word matches the jumble.
1 change: 1 addition & 0 deletions CodingChallenge.UI/TodoChallenge/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./dist
5 changes: 5 additions & 0 deletions CodingChallenge.UI/TodoChallenge/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"singleQuote": true,
"bracketSpacing": true,
"printWidth": 120
}
19 changes: 9 additions & 10 deletions CodingChallenge.UI/TodoChallenge/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
# Ncontracts Coding Challenge

Please complete the following:
+ Implement addNewTodo in App.js
+ Fix the bug where text changes to a todo item are lost on browser refresh
+ Match the provided design (The design can be found in the same folder as this readme. The file is named todo-app-design.png)
+ Add a chart for complete and incomplete todo items
+ Replace the Edit/Save/Complete buttons with icons
+ Add a due date to each todo
+ Sort the todo list by due date
+ Make all of the tests pass
+ Refactor anything in this app to your liking

- Implement addNewTodo in App.js
- Fix the bug where text changes to a todo item are lost on browser refresh
- Match the provided design (The design can be found in the same folder as this readme. The file is named todo-app-design.png)
- Add a chart for complete and incomplete todo items
- Replace the Edit/Save/Complete buttons with icons
- Add a due date to each todo
- Sort the todo list by due date
- Make all of the tests pass
- Refactor anything in this app to your liking

## Getting Started with Create React App

Expand All @@ -32,4 +32,3 @@ You will also see any lint errors in the console.

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

8 changes: 8 additions & 0 deletions CodingChallenge.UI/TodoChallenge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"@testing-library/user-event": "^12.1.10",
"node-sass": "^5.0.0",
"react": "^17.0.2",
"react-datepicker": "^4.21.0",
"react-dom": "^17.0.2",
"react-feather": "^2.0.10",
"react-redux": "^7.2.4",
"react-scripts": "4.0.3",
"redux": "^4.1.0",
Expand All @@ -17,6 +19,7 @@
},
"scripts": {
"start": "react-scripts start",
"format": "npx prettier --write .",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
Expand All @@ -43,5 +46,10 @@
"@babel/plugin-syntax-jsx": "^7.12.13",
"prop-types": "^15.7.2",
"redux-devtools": "^3.7.0"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}
7 changes: 2 additions & 5 deletions CodingChallenge.UI/TodoChallenge/public/index.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
Expand Down
69 changes: 46 additions & 23 deletions CodingChallenge.UI/TodoChallenge/src/App.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
import React, {Component} from 'react';
import TodoList from "./components/todo/TodoList";
import "./App.scss";
import React, { useState } from 'react';
import DatePicker from 'react-datepicker';
import "react-datepicker/dist/react-datepicker.css";
import { useDispatch, useSelector } from 'react-redux';
import TodoList from './components/todo/TodoList';
import { addTodo } from './todoActions';
import './App.scss';
import './button.scss';
import PieChartComponent from "./components/PieChart";

class App extends Component {
constructor(props) {
super(props);
const App = () => {
const dispatch = useDispatch();
const todos = useSelector((state) => state.todos ?? []);

this.state = {
newTodo: ''
};
}
const [text, setText] = useState('');
const [dueDate, setDueDate] = useState(new Date());

textInputChange = (e) => {
this.setState({...this.state, newTodo: e.target.value});
}
const handleInputChange = (e) => {
setText(e.target.value);
};

addNewTodo = () => {
console.warn('not implemented');
const handleDateChange = (date) => {
setDueDate(date);
}

render() {
return (
<div className="App">
<input type="text" value={this.state.newTodo} onChange={this.textInputChange}></input>
<button className={"btn--default"} onClick={this.addNewTodo}>Add</button>
<TodoList />
const addNewTodo = () => {
dispatch(addTodo({ text, dueDate }));
setText('');
setDueDate(new Date());
};

return (
<div className="App">
<h2>Todo App</h2>
<div className="header-section">
<PieChartComponent todos={todos} />
<div className="control-bar">
<input
type="text"
value={text}
onChange={handleInputChange}
className="text"
placeholder="Enter todo..."
/>
<DatePicker selected={dueDate} onChange={handleDateChange} />
<button className="btn--default" onClick={addNewTodo}>
Add
</button>
</div>
</div>
)}
}
<TodoList />
</div>
);
};

export default App;
Loading