0% found this document useful (0 votes)
4 views4 pages

SQL ProblemSolving SystemDesign Questions

The document contains SQL queries for finding the second-highest salary and counting employees by department, along with Python functions to check for palindromes and find the longest common prefix. It also outlines a system design for a URL shortener, detailing the database schema and Python implementation for shortening and retrieving URLs. The solutions provided demonstrate practical applications of SQL and programming concepts for problem-solving.

Uploaded by

GAMEPLAY MASTER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

SQL ProblemSolving SystemDesign Questions

The document contains SQL queries for finding the second-highest salary and counting employees by department, along with Python functions to check for palindromes and find the longest common prefix. It also outlines a system design for a URL shortener, detailing the database schema and Python implementation for shortening and retrieving URLs. The solutions provided demonstrate practical applications of SQL and programming concepts for problem-solving.

Uploaded by

GAMEPLAY MASTER
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL, Problem Solving, and System Design Questions

SQL Questions

1. Write a query to find the second-highest salary in a table named `employees`.

```sql

SELECT MAX(salary) AS second_highest_salary

FROM employees

WHERE salary < (SELECT MAX(salary) FROM employees);

```

2. Write a query to count the number of employees in each department.

```sql

SELECT department_id, COUNT(*) AS employee_count

FROM employees

GROUP BY department_id;

```

Problem-Solving Questions

1. Write a Python function to check if a string is a palindrome.

```python

def is_palindrome(s):

return s == s[::-1]

# Example
SQL, Problem Solving, and System Design Questions

print(is_palindrome('madam')) # Output: True

```

2. Write a Python program to find the longest common prefix among a list of strings.

```python

def longest_common_prefix(strings):

if not strings:

return ""

prefix = strings[0]

for string in strings[1:]:

while not string.startswith(prefix):

prefix = prefix[:-1]

if not prefix:

return ""

return prefix

# Example

print(longest_common_prefix(['flower', 'flow', 'flight'])) # Output: 'fl'

```

System Design Question

1. Design a URL Shortener system.

- Use an incremental counter for unique IDs.


SQL, Problem Solving, and System Design Questions

- Convert IDs to base62 for short URLs.

- Store mappings in a database.

**Database Schema:**

```sql

CREATE TABLE url_mapping (

id SERIAL PRIMARY KEY,

long_url TEXT NOT NULL,

short_url VARCHAR(10) UNIQUE NOT NULL,

created_at TIMESTAMP DEFAULT NOW()

);

```

**Python Implementation:**

```python

class URLShortener:

def __init__(self):

self.url_map = {}

self.counter = 1

self.base_url = "http://short.ly/"

def shorten(self, long_url):

short_url = self.base_url + str(self.counter)

self.url_map[short_url] = long_url

self.counter += 1
SQL, Problem Solving, and System Design Questions

return short_url

def retrieve(self, short_url):

return self.url_map.get(short_url, "URL not found")

```

You might also like