DEV Community

Cover image for Invalid Tweets | LeetCode | MSSQL
Retiago Drago
Retiago Drago

Posted on

1

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!

Output

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
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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

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.

favicon beacons.ai

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay