<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Whitney</title>
    <description>The latest articles on Forem by Whitney (@whitneywind).</description>
    <link>https://forem.com/whitneywind</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1473579%2F0cd0149a-cb12-4374-bd23-d28a74526e17.jpeg</url>
      <title>Forem: Whitney</title>
      <link>https://forem.com/whitneywind</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/whitneywind"/>
    <language>en</language>
    <item>
      <title>Algorithm Techniques: Two Pointers</title>
      <dc:creator>Whitney</dc:creator>
      <pubDate>Wed, 15 May 2024 15:53:44 +0000</pubDate>
      <link>https://forem.com/whitneywind/algorithm-techniques-two-pointers-43n5</link>
      <guid>https://forem.com/whitneywind/algorithm-techniques-two-pointers-43n5</guid>
      <description>&lt;h2&gt;
  
  
  What is the two pointers technique?
&lt;/h2&gt;

&lt;p&gt;A more flexible variation of the sliding window technique, the two pointers used in this technique can move independently or in tandem through data structures. &lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When is this technique useful?
&lt;/h2&gt;

&lt;p&gt;This is a common pattern that can be used in a wide range of algorithms. Each pointer refers to an index and travels through the array/string/linked list in order to solve a problem. If the problem requires finding pairs, a specific subsequence, or checking for a palindrome, consider using the two pointer technique. &lt;/p&gt;

&lt;p&gt;In the examples below, two pointers are used to accomplish a variety of tasks.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common examples:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sorting Colors (with JS)
This function uses two pointers to sort an array of integers in-place.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sortColors(nums: number[]): void {
    // two pointers
    let left = 0, mid = 0;
    let right = nums.length - 1;

    while (mid &amp;lt;= right) {
        if (nums[mid] === 0) {
            [nums[left], nums[mid]] = [nums[mid], nums[left]];
            left++;
            mid++;
        } else if (nums[mid] === 2) {
            [nums[mid], nums[right]] = [nums[right], nums[mid]];
            right--;
        } else {
            mid++;
        }
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Merging Arrays
In this simple array merging algorithm, a two pointers travel down two separate sorted arrays, while a third one inserts the larger of the pair. This maintains the nondecreasing order while merging.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function merge(nums1: number[], m: number, nums2: number[], n: number): void {
    let mPointer = m - 1;
    let nPointer = n - 1;
    let insertionInd = m + n - 1;

    while (nPointer &amp;gt;= 0) {
        if (nums1[mPointer] &amp;gt; nums2[nPointer]) {
            nums1[insertionInd] = nums1[mPointer];
            mPointer--;
        } else {
            nums1[insertionInd] = nums2[nPointer];
            nPointer--;
        }
        insertionInd--;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finding Palindromic Substrings
Finally, two pointers are used in this example to identify palindromes. After successfully identifying one, the pointers move to the left and right sides to search for an even longer palindrome.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countSubstrings(s: string): number {
    let count = 0;

    for (let i = 0; i &amp;lt; s.length; i++) {
        expand(i, i); // check for odd-length palindromes
        expand(i, i + 1); // check for even-length palindromes
    }
    return count;

    function expand(left, right) {
        while (left &amp;gt;= 0 &amp;amp;&amp;amp; right &amp;lt; s.length &amp;amp;&amp;amp; s[left] === s[right]) {
            count++;
            left--;
            right++;
        }
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
    </item>
    <item>
      <title>Algorithm Techniques: Sliding Window</title>
      <dc:creator>Whitney</dc:creator>
      <pubDate>Mon, 06 May 2024 23:49:47 +0000</pubDate>
      <link>https://forem.com/whitneywind/algorithm-techniques-sliding-window-58jd</link>
      <guid>https://forem.com/whitneywind/algorithm-techniques-sliding-window-58jd</guid>
      <description>&lt;h2&gt;
  
  
  What is the sliding window technique?
&lt;/h2&gt;

&lt;p&gt;The sliding window is an algorithm method that iterates a range, or "window" through a sequential data structure. This is an efficient way to process elements using only one loop.&lt;br&gt;&lt;br&gt;
 &lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  When is this technique useful?
&lt;/h2&gt;

&lt;p&gt;As an alternative to nested loops, the sliding window can reduce the time complexity from O(n^2) to O(n). It is useful when solving problems that require finding a subrange or subsequence. For example, when finding a subarray with the highest sum or the longest substring without repeating characters. &lt;br&gt;
&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Common problems this technique solves:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Finding the longest substring without repeating characters
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// typescript solution:

function lengthOfLongestSubstring(s: string): number {
    let max = 0;
    let start = 0;
    const map = new Map();
    for (let i = 0; i &amp;lt; s.length; i++) {
        if (map.has(s[i])) {
            start = Math.max(map.get(s[i]) + 1, start);
        }
        map.set(s[i], i);
        max = Math.max(max, i - start + 1);
    }
    return max;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Finding the smallest subarray with a minimum sum
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// typescript solution: 

function minSubArrayLen(target: number, nums: number[]): number {
  let minLength = Infinity;
  let left = 0, right = 0, sum = 0;

  while (right &amp;lt; nums.length) {
    sum += nums[right];

    while (sum &amp;gt;= target) {
        minLength = Math.min(minLength, right - left + 1);
        if (minLength === 1) return minLength; 
        sum -= nums[left];
        left++;
    }
    right++;
  }

  return minLength === Infinity ? 0 : minLength;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>algorithms</category>
      <category>slidingwindow</category>
    </item>
    <item>
      <title>Minor Issues: Handling user events with Jest</title>
      <dc:creator>Whitney</dc:creator>
      <pubDate>Mon, 06 May 2024 21:23:18 +0000</pubDate>
      <link>https://forem.com/whitneywind/minor-issues-handling-user-events-with-jest-46lf</link>
      <guid>https://forem.com/whitneywind/minor-issues-handling-user-events-with-jest-46lf</guid>
      <description>&lt;p&gt;In an effort to document what I'm working on - and hopefully retain the information as well - I'll be documenting the tidbits that I learn. This includes only the information that I can share publicly, including my personal projects and open-source work. Feel free to make suggestions where you see fit! &lt;/p&gt;

&lt;h2&gt;
  
  
  Jest testing with React
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Jest is a popular JavaScript testing framework known for unit and integration testing. This week I used Jest and React Testing Library to test an open-source codebase's reusable button component. Tests are being implemented retroactively and aim to better protect the codebase. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of my the button testing went smoothly. Testing for focus, however, proved to be a challenge.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Problem: Testing that a button doesn't trap focus&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One test needed to assert that a "Tab" press would move focus from the button to the next element. At first, I tried using "fireEvent" to mimic a keyboard press of the Tab key. However, this did not affect the focus. I learned that fireEvent is not the best way to do this.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Solution: @testing-library/user-event&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;FireEvent can be used with Jest tests, but in most cases it is recommended to use @testing-library/user-event instead. &lt;/p&gt;

&lt;p&gt;The @testing-library/user-event library mimics user interactions, accounts for accessibility best practices, and manages edge cases where fireEvent may not behave as expected - like in this case!&lt;/p&gt;

&lt;p&gt;In this case, userEvent.tab() is all that was needed to successfully imitate a user hitting "Tab" on the keyboard.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test('Button does not trap focus', () =&amp;gt; {
  const { getByRole, getByTestId } = render(
    &amp;lt;&amp;gt;
      &amp;lt;Button buttonText="Click me" /&amp;gt;
      &amp;lt;input type="text" data-testid="next-element" /&amp;gt;
    &amp;lt;/&amp;gt;
  );

  const button = getByRole('button', { name: /Click me/i });
  const nextElement = getByTestId('next-element');

  button.focus();
  userEvent.tab();

  expect(nextElement).toHaveFocus();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
