<?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>DEV Community: Cristian Malaga</title>
    <description>The latest articles on DEV Community by Cristian Malaga (@cmalaga).</description>
    <link>https://dev.to/cmalaga</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%2F3251466%2F293165b5-4a78-428d-9442-70a60f84167b.jpg</url>
      <title>DEV Community: Cristian Malaga</title>
      <link>https://dev.to/cmalaga</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cmalaga"/>
    <language>en</language>
    <item>
      <title>Reversing a String with Constant Space Complexity in C++</title>
      <dc:creator>Cristian Malaga</dc:creator>
      <pubDate>Fri, 30 Jan 2026 00:26:07 +0000</pubDate>
      <link>https://dev.to/cmalaga/reversing-a-string-with-constant-space-complexity-in-c-49gf</link>
      <guid>https://dev.to/cmalaga/reversing-a-string-with-constant-space-complexity-in-c-49gf</guid>
      <description>&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;Reversing a string in C++ while maintaining constant space complexity is a straightforward task. The key requirement is that the algorithm’s memory usage remains unchanged, regardless of the input string’s length. Whether the string contains a single character or thousands, the space allocated does not increase in accordance with the size of the string. This efficiency is achieved by modifying the string elements directly, performing all operations in place.&lt;/p&gt;

&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;

&lt;p&gt;The most effective method for this task is the two-pointer technique. This approach is widely recognized for its efficiency in solving algorithmic problems and typically provides linear time complexity—a significant improvement over methods that use nested loops and result in quadratic time complexity. The two-pointer strategy uses two variables to track positions within the string, allowing elements to be swapped directly without allocating additional space in proportion to the input string.&lt;/p&gt;

&lt;h1&gt;
  
  
  Implementation
&lt;/h1&gt;

&lt;p&gt;In practice, the left pointer is initialized at the start of the array, while the right pointer is set at the end. These pointers reference their respective index positions in the array. The core condition for iteration is that the left pointer remains less than the right pointer, ensuring that swaps occur only between distinct elements. During each iteration, a temporary variable is declared and initialized to store the value of the left element, facilitating a safe swap without data loss. After swapping, the left pointer is incremented and the right pointer is decremented, guiding both pointers toward the center of the array. It is crucial to assign the value to the right element from the temporary variable only after the left element has taken its new value.&lt;/p&gt;

&lt;p&gt;It is important to note that you can use the built in C++ reverse function in the standard namespace that basically does the same thing. If you wish to use this convenient function all you must do is directly include the algorithm header or indirectly include it via other headers. The time complexity and space complexity are the same as my implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reverseString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;size_t&lt;/span&gt; &lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;char&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;left&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;right&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;reverseStringSTL&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;begin&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>cpp</category>
      <category>programming</category>
    </item>
    <item>
      <title>How To Solve LeetCode 1193</title>
      <dc:creator>Cristian Malaga</dc:creator>
      <pubDate>Thu, 15 Jan 2026 20:45:46 +0000</pubDate>
      <link>https://dev.to/cmalaga/how-to-solve-leetcode-1193-3ekp</link>
      <guid>https://dev.to/cmalaga/how-to-solve-leetcode-1193-3ekp</guid>
      <description>&lt;p&gt;This problem begins with a table of transactions that has the following column names: id, country, state, amount, and trans_date. As explained, the id is the primary key, and the table contains information on incoming transactions. When looking at this, it is important to note that the state column is an enumeration that categorizes the transaction as approved or declined. We are required to return the result set in any order with specific aggregate columns, such as: month (year-month), trans_count (total transaction count per grouping), approved_count (count of approved transactions per grouping), trans_total_amount (sum of transaction amounts per grouping), and approved_total_amount (sum of approved transaction amounts per grouping).&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;p&gt;My approach is to first identify the necessary grouping, which in this case is by the month-year of the transaction date and the country. All of the grouping can be achieved in a single GROUP BY query utilizing aggregate functions like COUNT() and SUM(). For some of the aggregate functions, it is necessary to use CASE statements to properly return the correct aggregation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Aggregate Columns
&lt;/h2&gt;

&lt;p&gt;To calculate the month and year, I convert the transaction date into a string using the TO_CHAR(, ) function in PostgreSQL. The string representation I used is ‘YYYY-MM’, and the date column is trans_date. This built-in function returns the string representation of the year and month. Furthermore, to get the total transaction count (trans_count), you simply use the COUNT() aggregate function and pass in the transaction id as an argument. The transaction total amount (trans_total_amount) is similar; all that is necessary is to pass the amount column into the SUM() function to get the sum for the grouped data.&lt;/p&gt;

&lt;p&gt;For the approved count (approved_count), I used the aggregate SUM() function with a CASE statement inside as an argument. This works because the engine first evaluates the CASE statement and returns the result, and then applies the SUM function to the grouped data by the columns specified in the GROUP BY clause. Note that in the CASE statement, it is important to return 1 if the state is ‘approved’ and 0 otherwise, so that when the SUM function is applied, it will yield the count. The approved total amount (approved_total_amount) is similar to the approved count, with the only difference being what is returned in the CASE statement if the state is ‘approved’. The amount is returned in the CASE statement to get the sum of all transaction amounts per the grouping where the state is approved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; 
    &lt;span class="n"&gt;TO_CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trans_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'YYYY-MM'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;month&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;country&lt;/span&gt;                        &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                      &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;trans_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;CASE&lt;/span&gt;
            &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="nv"&gt;"state"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'approved'&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
        &lt;span class="k"&gt;END&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;                              &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;approved_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                    &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;trans_total_amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;CASE&lt;/span&gt;
            &lt;span class="k"&gt;WHEN&lt;/span&gt; &lt;span class="nv"&gt;"state"&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'approved'&lt;/span&gt; &lt;span class="k"&gt;THEN&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;
            &lt;span class="k"&gt;ELSE&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; 
        &lt;span class="k"&gt;END&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;                              &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;approved_total_amount&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Transactions&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;TO_CHAR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;trans_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'YYYY-MM'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>sql</category>
    </item>
    <item>
      <title>How To Solve LeetCode 596</title>
      <dc:creator>Cristian Malaga</dc:creator>
      <pubDate>Thu, 15 Jan 2026 17:53:06 +0000</pubDate>
      <link>https://dev.to/cmalaga/how-to-solve-leetcode-596-m8i</link>
      <guid>https://dev.to/cmalaga/how-to-solve-leetcode-596-m8i</guid>
      <description>&lt;p&gt;The problem begins by explaining there is a Courses table with two columns: student and class. The class column is qualitative and categorical, used to tag multiple rows with a predefined class type. The composite key for the table is made up of both the student and class columns. The goal is to return a result set of classes that have five or more students, in any order.&lt;/p&gt;

&lt;p&gt;This problem is simple and is solved using the GROUP BY clause followed by the HAVING clause. My approach is to use the SELECT clause to choose the column the problem wants, and then use the GROUP BY clause to group the classes so there is one row per class.&lt;/p&gt;

&lt;p&gt;Next, I use the HAVING clause with the aggregate function COUNT() to filter the grouped data. The goal is to filter the result set to include only classes where the count of students is greater than or equal to five. The HAVING clause is essential as it allows a programmer to filter aggregated data using aggregate functions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Courses&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;student&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>sql</category>
    </item>
    <item>
      <title>How To Solve LeetCode 586</title>
      <dc:creator>Cristian Malaga</dc:creator>
      <pubDate>Thu, 15 Jan 2026 03:25:22 +0000</pubDate>
      <link>https://dev.to/cmalaga/how-to-solve-leetcode-586-2k8i</link>
      <guid>https://dev.to/cmalaga/how-to-solve-leetcode-586-2k8i</guid>
      <description>&lt;p&gt;This problem asks you to find the customer number that has placed the largest quantity of orders. The customer number is the identifier for the customer in the orders table as explained in the problem. My first intuition was to look at the desired output. According to the problem description, the test cases will only produce exactly one customer with the most orders. It is important to note that at the end of the problem there is a follow up question asking what if more than one customer has the largest number of orders.&lt;/p&gt;

&lt;p&gt;With that said, let's break this problem down into smaller, modular pieces. The orders table simply contains two columns that are both identifiers, order number and customer number as previously stated. My main goal to solve this problem is to aggregate the data to get a count of orders per the respective customer id. In addition, I want to also use a window function to rank the order counts just in case there are multiple customers tied with the max order count. &lt;/p&gt;

&lt;h2&gt;
  
  
  CTE (Common Table Expression)
&lt;/h2&gt;

&lt;p&gt;Thus, we can easily aggregate the data using the GROUP BY clause counting the number of orders per customer by using the aggregate function COUNT(). This aggregation will form our first Common Table Expression (CTE), which will be used in the next CTE to assign a rank using the entire result set as a partition.&lt;/p&gt;

&lt;p&gt;In the next Common Table Expression,  I use the RANK() function to rank the rows per a specific partition. The partition that I chose was the whole result set. Since I omitted the PARTITION BY keyword, the data is being partitioned by the whole result set. The result set is ordered in descending order by the order count column, so the count of the most orders will receive a ranking of 1. In the unlikely case that there is a tie with customers then the rank will assign a 1 to both customer rows. This safeguards the solution in case there is a tie for the maximum number of orders.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In the last step, I bring it all together by querying the customer_order_rank CTE and only returning the customer identifier for the rows where the rank is 1 in the SELECT clause. I solved this problem using the PostgreSQL dialect of SQL, below you can reference the syntax. It is important to note that the OVER keyword is used to tell the database engine what you are ranking, and in this scenario I am ranking the partition of the whole result set ordered by the order count descending.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="n"&gt;customer_order_count&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customer_number&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;customer_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;order_number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_count&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Orders&lt;/span&gt;
    &lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;customer_number&lt;/span&gt;
&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="n"&gt;customer_order_rank&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customer_number&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;customer_number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;RANK&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;order_count&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;order_rnk&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customer_order_count&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;customer_number&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;customer_order_rank&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;order_rnk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>sql</category>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
