Skip to content

Instantly share code, notes, and snippets.

@carefree-ladka
Created August 12, 2023 17:46
Show Gist options
  • Select an option

  • Save carefree-ladka/0f2a5e03fdb79be2b93bbe2196a907fc to your computer and use it in GitHub Desktop.

Select an option

Save carefree-ladka/0f2a5e03fdb79be2b93bbe2196a907fc to your computer and use it in GitHub Desktop.
function findRepeatingIndex(string) {
const map = {};
for (let i = 0; i < string.length; i++) {
const char = string[i];
if (!map[char]) {
map[char] = { startingIndex: i };
} else {
map[char].endIndex = i;
}
}
const result = [];
for (const [char, charIdx] of Object.entries(map)) {
const { startingIndex, endIndex } = charIdx;
if (endIndex > 0) {
result.push([startingIndex, endIndex]);
}
}
return result;
}
console.log(findRepeatingIndex("hellooooloo"));
const dictionary = {
hellolo: true,
};
function canBeFormed(input) {
if (input in dictionary) {
return true;
}
for (let i = 0; i < input.length; i++) {
if (input[i] === input[i + 1]) {
const newInput = input.slice(0, i) + input.slice(i + 1);
if (canBeFormed(newInput)) {
return true;
}
}
}
return false;
}
const input = "hellooooloo";
const op = canBeFormed(input);
console.log(op); // true
const dict = {
hi: true,
hello: true,
world: true,
};
function spaceSeparator(input) {
if (input === "") {
return "";
}
for (let i = 1; i <= input.length; i++) {
const word = input.slice(0, i);
if (word in dict) {
const rest = spaceSeparator(input.slice(i));
if (rest !== null) {
return word + " " + rest;
}
}
}
return null;
}
const str = spaceSeparator("helloworld"); // "hello world"
const str2 = spaceSeparator("helloworldhi"); // "hello world hi"
const str3 = spaceSeparator("helloworldh"); // ""
console.log(str);
console.log(str2);
console.log(str3);
const word = "example";
const threshold = 1000000000;
const windowSize = 3600; // 1 hour in seconds
const counts = new Array(windowSize).fill(0);
let totalCount = 0;
function processTweet(tweet) {
if (tweet.includes(word)) {
const timestamp = Math.floor(Date.now() / 1000); // current timestamp in seconds
const index = timestamp % windowSize; // index in the counts array
totalCount -= counts[index]; // subtract the count at the index from the total count
counts[index] = tweet.split(word).length - 1; // count the number of occurrences of the word in the tweet
totalCount += counts[index]; // add the new count to the total count
if (totalCount >= threshold) {
console.log(
`Alert: ${word} repeated ${totalCount} times in the last hour`
);
}
}
}
// Example usage:
processTweet("This is an example tweet");
processTweet("Another example tweet with the word example");
class LRUCache {
constructor(maxSize, expiryTime) {
this.maxSize = maxSize;
this.expiryTime = expiryTime;
this.cache = new Map();
this.head = null;
this.tail = null;
}
async get(key, fn) {
const entry = this.cache.get(key);
if (entry) {
if (Date.now() < entry.expiryTime) {
this.moveToHead(entry);
return entry.value;
} else {
this.removeEntry(entry);
}
}
const value = await fn();
const newEntry = { key, value, expiryTime: Date.now() + this.expiryTime };
this.cache.set(key, newEntry);
this.addToHead(newEntry);
if (this.cache.size > this.maxSize) {
const removedEntry = this.removeTail();
console.log(`Cache burst: removed entry with key ${removedEntry.key}`);
}
return value;
}
addToHead(entry) {
entry.prev = null;
entry.next = this.head;
if (this.head) {
this.head.prev = entry;
} else {
this.tail = entry;
}
this.head = entry;
}
moveToHead(entry) {
if (entry === this.head) {
return;
}
if (entry === this.tail) {
this.tail = entry.prev;
this.tail.next = null;
} else {
entry.prev.next = entry.next;
entry.next.prev = entry.prev;
}
this.addToHead(entry);
}
removeEntry(entry) {
if (entry === this.head) {
this.head = entry.next;
} else if (entry === this.tail) {
this.tail = entry.prev;
this.tail.next = null;
} else {
entry.prev.next = entry.next;
entry.next.prev = entry.prev;
}
this.cache.delete(entry.key);
}
removeTail() {
const entry = this.tail;
this.tail = entry.prev;
if (this.tail) {
this.tail.next = null;
} else {
this.head = null;
}
this.cache.delete(entry.key);
return entry;
}
}
// Example usage:
const cache = new LRUCache(3, 10000); // max size of 3 entries, expiry time of 10 seconds
async function fetchData(key) {
console.log(`Fetching data for key ${key}`);
return new Promise((resolve) =>
setTimeout(() => resolve(`Data for key ${key}`), 1000)
);
}
console.log(await cache.get("key1", () => fetchData("key1")));
console.log(await cache.get("key2", () => fetchData("key2")));
console.log(await cache.get("key3", () => fetchData("key3")));
console.log(await cache.get("key1", () => fetchData("key1"))); // should hit cache
console.log(await cache.get("key4", () => fetchData("key4"))); // should trigger cache burst
console.log(await cache.get("key2", () => fetchData("key2"))); // should hit cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment