Round #658
10 May 2026, 08:33· csharp· qwen2.5-coder-7b-instruct· r13-clean-hints
- Complexity
- band 0
- ZP / UO / OBS
- 0 / 0 / 0
- Journey weight
- 0
- Lightshift ΔL
- —
Prompt
Use a CTE to find the longest chain of referrals in a referrals table.
Zero-point index (what the engine surfaced)
ZPI-ZPI-0007 | 2026-05-10T08:33:46Z
Use a CTE to find the longest chain of referrals in a referrals table.
ZERO POINT — 0 entries (structural invariants)
UNOBSERVED — 0 entries (implied truths)
OBSERVED — 0 entries (explicit in request)
COMPLEXITY │ 0 entries │ spread: 0° │ band: LOW
Baseline response (no ZPI)
To find the longest chain of referrals in a referrals table using a Common Table Expression (CTE) in C#, you can follow these steps:
1. **Define the Referrals Table**: Assume your referrals table has two columns: `ReferrerID` and `ReferreeID`.
2. **Create the CTE**: Use a recursive CTE to trace the chain of referrals.
3. **Find the Longest Chain**: Determine the longest chain by counting the number of referrals in each chain and selecting the maximum count.
Here's an example implementation:
```csharp
using System;
using System.Data;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "your_connection_string_here";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
// Define the CTE to find the longest chain of referrals
string cteQuery = @"
WITH ReferralChain AS (
SELECT
ReferrerID,
ReferreeID,
CAST(ReferrerID AS NVARCHAR(MAX)) AS Chain,
1 AS ChainLength
FROM
Referrals
WHERE
ReferrerID IS NULL -- Assuming ReferrerID is null for the initial referrers
UNION ALL
SELECT
r.ReferrerID,
r.ReferreeID,
rc.Chain + ', ' + CAST(r.ReferreeID AS NVARCHAR(MAX)),
rc.ChainLength + 1
FROM
Referrals r
INNER JOIN
ReferralChain rc ON r.ReferreeID = rc.ReferreeID
)
SELECT
Chain,
ChainLength
FROM
ReferralChain
ORDER BY
ChainLength DESC";
using (SqlCommand command = new SqlCommand(cteQuery, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string chain = reader.GetString(0);
int chainLength = reader.GetInt32(1);
Console.WriteLine($"Chain: {chain}, Length: {chainLength}");
}
}
}
}
}
}
```
### Explanation:
1. **CTE Definition**:
- The `ReferralChain` CTE starts with the initial referrers (where `ReferrerID` is null) and assigns them a chain length of 1.
- It then recursively joins the referrals table to trace the chain, appending each referree ID to the chain string and incrementing the chain length.
2. **Finding the Longest Chain**:
- The final query selects all chains from the CTE and orders them by `ChainLength` in descending order.
- The longest chain will be at the top of the result set.
### Notes:
- Ensure that your connection string is correctly configured to connect to your database.
- Adjust the table and column names if they differ from the example.
- This example assumes that `ReferrerID` is null for initial referrers. If your schema uses a different approach, adjust the query accordingly.
This solution will help you find the longest chain of referrals in your referrals table using C#.
Enhanced response (with ZPI)
To find the longest chain of referrals in a referrals table using a Common Table Expression (CTE) in C#, you can follow these steps:
1. **Define the Referrals Table**: Assume you have a `Referrals` table with columns `ReferrerId` and `ReferreeId`.
2. **Create the CTE**: Use a CTE to recursively find all referrals for each user.
3. **Find the Longest Chain**: Determine the longest chain of referrals by counting the number of referrals in each chain.
Here's how you can implement this:
```csharp
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
class Program
{
static void Main()
{
string connectionString = "your_connection_string_here";
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
int longestChainLength = FindLongestReferralChain(connection);
Console.WriteLine($"The longest chain of referrals is: {longestChainLength}");
}
}
static int FindLongestReferralChain(SqlConnection connection)
{
string query = @"
WITH ReferralChains AS (
SELECT
ReferreeId,
1 AS ChainLength
FROM
Referrals
UNION ALL
SELECT
r.ReferreeId,
rc.ChainLength + 1
FROM
Referrals r
INNER JOIN
ReferralChains rc ON r.ReferrerId = rc.ReferreeId
)
SELECT MAX(ChainLength) AS LongestChainLength
FROM ReferralChains";
using (SqlCommand command = new SqlCommand(query, connection))
{
object result = command.ExecuteScalar();
if (result != null && result is int length)
{
return length;
}
}
return 0;
}
}
```
### Explanation:
1. **CTE Definition**:
- The `ReferralChains` CTE starts by selecting all users who have been referred (`ReferreeId`) and assigns them a chain length of 1.
- It then recursively joins the `Referrals` table with itself to find all subsequent referrals, incrementing the chain length for each level of referral.
2. **Finding the Longest Chain**:
- The main query selects the maximum chain length from the `ReferralChains` CTE.
3. **Error Handling and Execution**:
- The code uses a `using` block to ensure proper disposal of the database connection.
- It executes the query using a `SqlCommand` and retrieves the result, which is then converted to an integer representing the longest chain length.
This approach ensures that you find the longest chain of referrals efficiently using SQL's CTE feature.
Grader chain
Each grader reads the same baseline + enhanced pair and casts an independent verdict. Disagreement is the framework's own signal — not noise to be averaged away.
V1samba/Meta-Llama-3.3-70B-Instruct
BetterV4qwen2.5-coder-3b-instruct
BetterV5openrouter/qwen/qwen3-235b-a22b-2507
BetterV7qwen2.5-coder-7b-instruct
BetterV8openai/gpt-4.1
SameV9anthropic/claude-opus-4-7
SameV12openai/gpt-4o
BetterComments
Our grader said what it said. What do you say? Comment as a guest below.
No comments yet. Be the first to say what you make of this round.