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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file removed src/problem1/.keep
Empty file.
111 changes: 111 additions & 0 deletions src/problem1/advance_variants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
========================================================
Problem 1: Three ways to sum to n
========================================================

Task:
Provide 3 unique implementations of the following function in JavaScript.
Input: n - any integer
*Assuming this input will always produce a result lesser than `Number.MAX_SAFE_INTEGER`.
Output: return - summation to n, i.e. sum_to_n(5) === 1 + 2 + 3 + 4 + 5 === 15
========================================================
*/


/*--------------------------------------------------------
Implementation A — Iterative Approach
--------------------------------------------------------
Function Signature Example:
sum_to_n_a(5) => 15

Explanation:
Uses a loop to accumulate values from 1 to n.
Time Complexity: O(n)
Space Complexity: O(1)

*/
var sum_to_n_a = function(n) {
let sum = 0;
for (let i = 1; i <= n; i++) {
sum += i;
}
return sum;
};


/*--------------------------------------------------------
Implementation B — Formula Approach (Optimal)
--------------------------------------------------------
Function Signature Example:
sum_to_n_b(5) => 15

Explanation :
Uses Gauss’ summation formula: sum = n * (n + 1) / 2
No loop needed. Constant time solution.
Time Complexity: O(1)
Space Complexity: O(1)

*/
var sum_to_n_b = function(n) {
return n * (n + 1) / 2;
};


/*--------------------------------------------------------
Implementation C — Functional Approach
--------------------------------------------------------
Function Signature Example:
sum_to_n_c(5) => 15

Explanation :
Generates an array [1..n] and sums using reduce().
Demonstrates modern JavaScript and functional programming style.
Time Complexity: O(n)
Space Complexity: O(n)

*/
var sum_to_n_c = function(n) {
return Array.from({ length: n }, (_, i) => i + 1)
.reduce((acc, val) => acc + val, 0);
};


/*--------------------------------------------------------
Test Cases
--------------------------------------------------------
Verifies correctness of all 3 implementations
*/
function runTests() {
const testCases = [
{ input: 0, expected: 0 },
{ input: 1, expected: 1 },
{ input: 5, expected: 15 },
{ input: 10, expected: 55 },
{ input: 100, expected: 5050 }
];

const implementations = [
{ name: "Iterative", fn: sum_to_n_a },
{ name: "Formula", fn: sum_to_n_b },
{ name: "Functional", fn: sum_to_n_c }
];

console.log("Running Tests...\n");

testCases.forEach(({ input, expected }) => {
console.log(`Test n = ${input} (expected: ${expected})`);
implementations.forEach(({ name, fn }) => {
const result = fn(input);
const pass = result === expected;
console.log(
` ${name.padEnd(12)} → ${result} ${pass ? "✅ PASS" : "❌ FAIL"}`
);
});
console.log("");
});

console.log("All tests completed.");
}

// Execute test cases
runTests();
24 changes: 24 additions & 0 deletions src/problem2/fancy-swap/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
5 changes: 5 additions & 0 deletions src/problem2/fancy-swap/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Vue 3 + TypeScript + Vite

This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.

Learn more about the recommended Project Setup and IDE Support in the [Vue Docs TypeScript Guide](https://vuejs.org/guide/typescript/overview.html#project-setup).
13 changes: 13 additions & 0 deletions src/problem2/fancy-swap/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>problem2</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
Loading