Tasks Details
hard
Compute the total length covered by 1-dimensional segments.
Task Score
100%
Correctness
100%
Performance
Not assessed
You are given a table segments with the following structure:
create table segments ( l integer not null, r integer not null, check(l <= r), unique(l,r) );Each record in this table represents a contiguous segment of a line, from l to r inclusive. Its length equals r − l.
Consider the parts of a line covered by the segments. Write an SQL query that returns the total length of all the parts of the line covered by the segments specified in the table segments. Please note that any parts of the line that are covered by several overlapping segments should be counted only once.
For example, given:
l | r --+-- 1 | 5 2 | 3 4 | 6your query should return 5, as the segments cover the part of the line from 1 to 6.
Copyright 2009–2024 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used SQL (SQLite)
Total time used 4 minutes
Effective time used 4 minutes
Notes
not defined yet
Task timeline
Code: 18:21:02 UTC,
sql,
verify,
result: Failed
SELECT CASE WHEN (SELECT COUNT(*) FROM segments) = 0 THEN 0 ELSE
(
SELECT SUM(r - l) FROM
(
SELECT
(
SELECT MAX(x) FROM
(SELECT s2.l AS x FROM segments AS s2 WHERE s1.l = s2.l UNION
SELECT r AS x FROM segments AS s2 WHERE s2.l < s1.l)
) AS l,
r
FROM
(
SELECT l, MAX(r) AS r FROM segments GROUP BY l
) s1
)
WHERE r > l
)
-- Second attempt
Analysis
expand all
Example tests
1.
0.096 s
RUNTIME ERROR,
tested program terminated unexpectedly
stderr:
error on query: SELECT CASE WHEN (SELECT COUNT(*) FROM segments) = 0 THEN 0 ELSE ( SELECT SUM(r - l) FROM ( SELECT ( SELECT MAX(x) FROM (SELECT s2.l AS x FROM segments AS s2 WHERE s1.l = s2.l UNION SELECT r AS x FROM segments AS s2 WHERE s2.l < s1.l) ) AS l, r FROM ( SELECT l, MAX(r) AS r FROM segments GROUP BY l ) s1 ) WHERE r > l ) -- Second attempt, details: near "-- Second attempt": syntax error
Code: 18:21:13 UTC,
sql,
verify,
result: Passed
SELECT CASE WHEN (SELECT COUNT(*) FROM segments) = 0 THEN 0 ELSE
(
SELECT SUM(r - l) FROM
(
SELECT
(
SELECT MAX(x) FROM
(SELECT s2.l AS x FROM segments AS s2 WHERE s1.l = s2.l UNION
SELECT r AS x FROM segments AS s2 WHERE s2.l < s1.l)
) AS l,
r
FROM
(
SELECT l, MAX(r) AS r FROM segments GROUP BY l
) s1
)
WHERE r > l
)
END
-- Second attempt
Analysis
Code: 18:21:18 UTC,
sql,
verify,
result: Passed
SELECT CASE WHEN (SELECT COUNT(*) FROM segments) = 0 THEN 0 ELSE
(
SELECT SUM(r - l) FROM
(
SELECT
(
SELECT MAX(x) FROM
(SELECT s2.l AS x FROM segments AS s2 WHERE s1.l = s2.l UNION
SELECT r AS x FROM segments AS s2 WHERE s2.l < s1.l)
) AS l,
r
FROM
(
SELECT l, MAX(r) AS r FROM segments GROUP BY l
) s1
)
WHERE r > l
)
END
-- Second attempt
Analysis
Code: 18:21:21 UTC,
sql,
final,
score: 
100
SELECT CASE WHEN (SELECT COUNT(*) FROM segments) = 0 THEN 0 ELSE
(
SELECT SUM(r - l) FROM
(
SELECT
(
SELECT MAX(x) FROM
(SELECT s2.l AS x FROM segments AS s2 WHERE s1.l = s2.l UNION
SELECT r AS x FROM segments AS s2 WHERE s2.l < s1.l)
) AS l,
r
FROM
(
SELECT l, MAX(r) AS r FROM segments GROUP BY l
) s1
)
WHERE r > l
)
END
-- Second attempt
Analysis summary
The solution obtained perfect score.
Analysis
expand all
Correctness tests
1.
0.095 s
OK
1.
0.095 s
OK
1.
0.095 s
OK
1.
0.094 s
OK
1.
0.095 s
OK
2.
0.095 s
OK
1.
0.093 s
OK
2.
0.092 s
OK
1.
0.093 s
OK
1.
0.096 s
OK
1.
0.094 s
OK
1.
0.095 s
OK
1.
0.095 s
OK
1.
0.784 s
OK
1.
0.803 s
OK
1.
0.104 s
OK
1.
0.104 s
OK