Retiago Drago

Posted on

Invalid Tweets | LeetCode | MSSQL

The Problem

Table: Tweets

Column Name Type
tweet_id int
content varchar

The `tweet_id` is the primary key for this table, which contains all the tweets in a social media app.

The task is to write an SQL query to find the IDs of the invalid tweets. A tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.

Here's an example to better illustrate the problem:

Input

Tweets table:

tweet_id content
1 Vote for Biden
2 Let us make America great again!

tweet_id
2

Explanation

Tweet 1 has a length of 14. It's a valid tweet.
Tweet 2 has a length of 32. It's an invalid tweet.

The Solution

In this post, we'll delve into two MSSQL solutions. Both effectively solve the problem but using different functions — highlighting their unique strengths, weaknesses, and performance metrics.

Source Code 1

The first solution uses the `LEN` function to calculate the length of the tweet content:

``````SELECT tweet_id
FROM Tweets
WHERE LEN(content) > 15
``````

This code's runtime is 1953ms, beating 19.62% of other submissions. Here's the performance snapshot:

Source Code 2

The second solution differs from the first by using the `DATALENGTH` function instead of `LEN`:

``````SELECT tweet_id
FROM Tweets
WHERE DATALENGTH(content) > 15
``````

This solution's runtime is 1522ms, beating 66.57% of other submissions. Here's the performance snapshot:

Conclusion

These two solutions effectively determine the invalid tweets based on the content length, with the second solution performing significantly better on LeetCode. However, performance may vary in real-world RDMS due to different factors, such as data volume, system hardware, SQL server configurations, etc.

Here's a ranking of these solutions based on LeetCode performance:

1. Source Code 2
2. Source Code 1

You can find the original problem at LeetCode.

For more insightful solutions and tech-related content, feel free to connect with me on my Beacons page.

ranggakd - Link in Bio & Creator Tools | Beacons

@ranggakd | center details summary summary Oh hello there I m a an Programmer AI Tech Writer Data Practitioner Statistics Math Addict Open Source Contributor Quantum Computing Enthusiast details center.

beacons.ai