{"id":2800,"date":"2025-02-27T03:02:13","date_gmt":"2025-02-27T03:02:13","guid":{"rendered":"https:\/\/codeinsightacademy.com\/blog\/?p=2800"},"modified":"2025-02-27T03:02:17","modified_gmt":"2025-02-27T03:02:17","slug":"dsa-assesment","status":"publish","type":"post","link":"https:\/\/codeinsightacademy.com\/blog\/dsa\/dsa-assesment\/","title":{"rendered":"DSA Assesment"},"content":{"rendered":"\n<p>Sure! Here are three easy-level <strong>DSA problems<\/strong> using <strong>Array, Linked List, and Map<\/strong>, along with their solutions in <strong>JavaScript<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2><strong>1. Array Problem (Easy)<\/strong><\/h2>\n\n\n\n<h3><strong>Problem: Find the Second Largest Element<\/strong><\/h3>\n\n\n\n<p>Given an array of integers, find the <strong>second largest<\/strong> element in the array.<br>If there is no second largest element, return <code>-1<\/code>.<\/p>\n\n\n\n<h3><strong>Example:<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Input: arr = [10, 5, 20, 8]\nOutput: 10\n\nInput: arr = [7, 7, 7]\nOutput: -1\n<\/pre>\n\n\n\n<h3><strong>Solution (JavaScript)<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function secondLargest(arr) {\n    if (arr.length &lt; 2) return -1;\n\n    let largest = -Infinity, secondLargest = -Infinity;\n\n    for (let num of arr) {\n        if (num > largest) {\n            secondLargest = largest;\n            largest = num;\n        } else if (num > secondLargest &amp;&amp; num &lt; largest) {\n            secondLargest = num;\n        }\n    }\n\n    return secondLargest === -Infinity ? -1 : secondLargest;\n}\n\n\/\/ Test Cases\nconsole.log(secondLargest([10, 5, 20, 8])); \/\/ Output: 10\nconsole.log(secondLargest([7, 7, 7])); \/\/ Output: -1\nconsole.log(secondLargest([3])); \/\/ Output: -1\n<\/pre>\n\n\n\n<p><strong>Time Complexity:<\/strong> O(n)O(n)<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2><strong>2. Linked List Problem (Easy)<\/strong><\/h2>\n\n\n\n<h3><strong>Problem: Find the Middle Node of a Linked List<\/strong><\/h3>\n\n\n\n<p>Given a <strong>singly linked list<\/strong>, return the <strong>middle node<\/strong>.<br>If there are two middle nodes, return the second one.<\/p>\n\n\n\n<h3><strong>Example:<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Input: 1 \u2192 2 \u2192 3 \u2192 4 \u2192 5\nOutput: 3\n\nInput: 1 \u2192 2 \u2192 3 \u2192 4 \u2192 5 \u2192 6\nOutput: 4\n<\/pre>\n\n\n\n<h3><strong>Solution (JavaScript)<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class ListNode {\n    constructor(val) {\n        this.val = val;\n        this.next = null;\n    }\n}\n\nfunction findMiddle(head) {\n    let slow = head, fast = head;\n\n    while (fast !== null &amp;&amp; fast.next !== null) {\n        slow = slow.next;  \/\/ Move slow one step\n        fast = fast.next.next;  \/\/ Move fast two steps\n    }\n\n    return slow.val;\n}\n\n\/\/ Helper function to create a linked list\nfunction createLinkedList(arr) {\n    let head = new ListNode(arr[0]);\n    let current = head;\n    for (let i = 1; i &lt; arr.length; i++) {\n        current.next = new ListNode(arr[i]);\n        current = current.next;\n    }\n    return head;\n}\n\n\/\/ Test Cases\nlet head1 = createLinkedList([1, 2, 3, 4, 5]);\nconsole.log(findMiddle(head1)); \/\/ Output: 3\n\nlet head2 = createLinkedList([1, 2, 3, 4, 5, 6]);\nconsole.log(findMiddle(head2)); \/\/ Output: 4\n<\/pre>\n\n\n\n<p><strong>Time Complexity:<\/strong> O(n)O(n)<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2><strong>3. Map Problem (Easy)<\/strong><\/h2>\n\n\n\n<h3><strong>Problem: Find First Non-Repeating Character<\/strong><\/h3>\n\n\n\n<p>Given a string <code>s<\/code>, find the <strong>first non-repeating character<\/strong> and return its index.<br>If all characters repeat, return <code>-1<\/code>.<\/p>\n\n\n\n<h3><strong>Example:<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Input: \"leetcode\"\nOutput: 0  \/\/ ('l' is the first unique character)\n\nInput: \"aabb\"\nOutput: -1  \/\/ (No unique character)\n<\/pre>\n\n\n\n<h3><strong>Solution (JavaScript)<\/strong><\/h3>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">function firstUniqueChar(s) {\n    let charCount = new Map();\n\n    \/\/ Count frequency of characters\n    for (let char of s) {\n        charCount.set(char, (charCount.get(char) || 0) + 1);\n    }\n\n    \/\/ Find the first unique character\n    for (let i = 0; i &lt; s.length; i++) {\n        if (charCount.get(s[i]) === 1) return i;\n    }\n\n    return -1;\n}\n\n\/\/ Test Cases\nconsole.log(firstUniqueChar(\"leetcode\")); \/\/ Output: 0\nconsole.log(firstUniqueChar(\"loveleetcode\")); \/\/ Output: 2\nconsole.log(firstUniqueChar(\"aabb\")); \/\/ Output: -1\n<\/pre>\n\n\n\n<p><strong>Time Complexity:<\/strong> O(n)O(n)<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>These problems are great for understanding <strong>Array<\/strong>, <strong>Linked List<\/strong>, and <strong>Map<\/strong> usage in JavaScript. Let me know if you need more! \ud83d\ude80<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sure! Here are three easy-level DSA problems using Array, Linked List, and Map, along with their solutions in JavaScript. 1. Array Problem (Easy) Problem: Find the Second Largest Element Given an array of integers, find the second largest element in the array.If there is no second largest element, return -1. Example: Solution (JavaScript) Time Complexity: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[39],"tags":[],"_links":{"self":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2800"}],"collection":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/comments?post=2800"}],"version-history":[{"count":1,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2800\/revisions"}],"predecessor-version":[{"id":2801,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/posts\/2800\/revisions\/2801"}],"wp:attachment":[{"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/media?parent=2800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/categories?post=2800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeinsightacademy.com\/blog\/wp-json\/wp\/v2\/tags?post=2800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}