Example: x = [0, 3, 0] y = [0, 5, 2]
Aligned by index, the 3 coordinates are [0,0], [3,5], [0,2]. The base of the triangle is 2, and the height is 3. The area of a triangle is (base * height)/2, so 3 * 2 / 2 = 3. All resulting areas will be whole numbers.
Problem Statement: An email campaign tracking platform maintains data on various campaigns and their email statistics. The task is to generate a report that lists each campaign's name along with the total number of emails sent, emails opened, and emails not opened.
The result should have the following columns: campaign_name | total_emails_sent | total_emails_opened | total_emails_not_opened.
campaign_name: The name of the email campaign.
total_emails_sent: The total number of emails sent.
total_emails_opened: The total number of emails opened.
total_emails_not_opened: The total number of emails not opened (calculated as the difference between sent and opened).
The result should be sorted in ascending order by campaign_name..
Problem Statement: The state of Hackerland is represented as a graph of g_nodes with each city numbered from 1 to g_nodes. There are g_edges bidirectional roads between the cities g_from[i] and g_to[i] with a repair cost of g_weight[i]. Initially, all roads are damaged. One can spend some amount of money, say X, and repair all the roads whose repair cost is less than or equal to X.
The task is to reach the city numbered g_nodes starting from city 1 by traveling no more than k roads while spending as little money as possible. Find the minimum money
Triangle Area Solution
Topics Involved-:
Coordinate Geometry
Basic Math
Overview
Approach
1. Finding the Base and Height
We can check pairs of coordinates to see if they align horizontally or vertically. If two points have the same x-coordinate, the distance between their y-coordinates is the base. The height is then the horizontal distance from the remaining point to that x-axis line. We apply the same logic if two points share a y-coordinate.
2. Calculating the Area
Once we have the base and the height, we just use the standard triangle area formula:(base*height)/2 Since the problem guarantees the result will be a whole number, we can safely use integer division.
Code Implementation (Pseudocode)
Time Complexity
Time: O(1) - We only check a maximum of three pairs of coordinates, so it runs in constant time.
Space: O(1) - Only a few variables are needed to store the base and height.
Problem-2 Email Campaign Tracker Solution
Topics Involved / Prerequisites
SQL
GROUP BYAggregate Functions (
SUM)Overview
Approach
1. Grouping and Aggregating Assuming the data is in a table named
campaign_data, we use theGROUP BY campaign_nameclause to condense multiple rows into a single row per campaign. We then use theSUM()function to add up all the sent and opened emails for each group.2. Deriving the Unopened Count SQL allows us to perform mathematical operations directly in the
SELECTstatement. We can find thetotal_emails_not_openedby simply doingSUM(emails_sent) - SUM(emails_opened)and using theASkeyword to alias our columns to the requested names. We finish with anORDER BYclause.SQL Query
Hackerland Road Repair Solution
Topics Involved / Prerequisites
Binary Search on Answer
Graph Theory (Breadth-First Search)
Overview
Xthat allows us to reach the destination withinksteps.Approach
1. Binary Search the Budget
Instead of trying every possible combination of roads, we guess a budget
X. IfXallows us to reach the destination in <=k steps, we record it as a potential answer and try a lower budget. IfXfails, we know we need a higher budget.2. BFS for Step Validation
To check if a budget
Xis valid, we run a Breadth-First Search (BFS) from city 1. We only traverse roads that have a repair cost <=X. We keep track of the minimum steps taken to reach each city. If we reachg_nodesinksteps or less, the budget is valid!Code Implementatin
Time Complexity
Time: O((V + E) log W) Where V is nodes, E is edges, and W is the maximum weight.
Space: O(V + E) - To store the graph in an adjacency list and for the BFS queue.