{ "data": { "question": { "questionId": "2743", "questionFrontendId": "2627", "boundTopicId": null, "title": "Debounce", "titleSlug": "debounce", "content": "

Given a function fn and a time in milliseconds t, return a debounced version of that function.

\n\n

debounced function is a function whose execution is delayed by t milliseconds and whose execution is cancelled if it is called again within that window of time. The debounced function should also recieve the passed parameters.

\n\n

For example, let's say t = 50ms, and the function was called at 30ms60ms, and 100ms. The first 2 function calls would be cancelled, and the 3rd function call would be executed at 150ms. If instead t = 35ms, The 1st call would be cancelled, the 2nd would be executed at 95ms, and the 3rd would be executed at 135ms.

\n\n

\"Debounce

\n\n

The above diagram shows how debounce will transform events. Each rectangle represents 100ms and the debounce time is 400ms. Each color represents a different set of inputs.

\n\n

Please solve it without using lodash's _.debounce() function.

\n\n

 

\n

Example 1:

\n\n
\nInput: \nt = 50\ncalls = [\n  {"t": 50, inputs: [1]},\n  {"t": 75, inputs: [2]}\n]\nOutput: [{"t": 125, inputs: [2]}]\nExplanation:\nlet start = Date.now();\nfunction log(...inputs) { \n  console.log([Date.now() - start, inputs ])\n}\nconst dlog = debounce(log, 50);\nsetTimeout(() => dlog(1), 50);\nsetTimeout(() => dlog(2), 75);\n\nThe 1st call is cancelled by the 2nd call because the 2nd call occurred before 100ms\nThe 2nd call is delayed by 50ms and executed at 125ms. The inputs were (2).\n
\n\n

Example 2:

\n\n
\nInput: \nt = 20\ncalls = [\n  {"t": 50, inputs: [1]},\n  {"t": 100, inputs: [2]}\n]\nOutput: [{"t": 70, inputs: [1]}, {"t": 120, inputs: [2]}]\nExplanation:\nThe 1st call is delayed until 70ms. The inputs were (1).\nThe 2nd call is delayed until 120ms. The inputs were (2).\n
\n\n

Example 3:

\n\n
\nInput: \nt = 150\ncalls = [\n  {"t": 50, inputs: [1, 2]},\n  {"t": 300, inputs: [3, 4]},\n  {"t": 300, inputs: [5, 6]}\n]\nOutput: [{"t": 200, inputs: [1,2]}, {"t": 450, inputs: [5, 6]}]\nExplanation:\nThe 1st call is delayed by 150ms and ran at 200ms. The inputs were (1, 2).\nThe 2nd call is cancelled by the 3rd call\nThe 3rd call is delayed by 150ms and ran at 450ms. The inputs were (5, 6).\n
\n\n

 

\n

Constraints:

\n\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Medium", "likes": 13, "dislikes": 1, "isLiked": null, "similarQuestions": "[{\"title\": \"Promise Time Limit\", \"titleSlug\": \"promise-time-limit\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Cache With Time Limit\", \"titleSlug\": \"cache-with-time-limit\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]", "exampleTestcases": "50\n[{\"t\":50,\"inputs\":[1]},{\"t\":75,\"inputs\":[2]}]\n20\n[{\"t\":50,\"inputs\":[1]},{\"t\":100,\"inputs\":[2]}]\n150\n[{\"t\":50,\"inputs\":[1,2]},{\"t\":300,\"inputs\":[3,4]},{\"t\":300,\"inputs\":[5,6]}]", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {Function} fn\n * @param {number} t milliseconds\n * @return {Function}\n */\nvar debounce = function(fn, t) {\n return function(...args) {\n \n }\n};\n\n/**\n * const log = debounce(console.log, 100);\n * log('Hello'); // cancelled\n * log('Hello'); // cancelled\n * log('Hello'); // Logged at t=100ms\n */", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "type F = (...p: any[]) => any\n\nfunction debounce(fn: F, t: number): F {\n return function(...args) {\n \n }\n};\n\n/**\n * const log = debounce(console.log, 100);\n * log('Hello'); // cancelled\n * log('Hello'); // cancelled\n * log('Hello'); // Logged at t=100ms\n */", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"351\", \"totalSubmission\": \"379\", \"totalAcceptedRaw\": 351, \"totalSubmissionRaw\": 379, \"acRate\": \"92.6%\"}", "hints": [ "You execute code with a delay with \"ref = setTimeout(fn, delay)\". You can abort the execution of that code with \"clearTimeout(ref)\"", "Whenever you call the function, you should abort any existing scheduled code. Then, you should schedule code to be executed after some delay." ], "solution": null, "status": null, "sampleTestCase": "50\n[{\"t\":50,\"inputs\":[1]},{\"t\":75,\"inputs\":[2]}]", "metaData": "{\n \"name\": \"debounce\",\n \"params\": [\n {\n \"name\": \"fn\",\n \"type\": \"string\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"t\"\n }\n ],\n \"return\": {\n \"type\": \"string\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.

\"], \"typescript\": [\"Typescript\", \"

TypeScript 4.5.4, Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }