Latest New Job
Updated
Problem statement
A job table has one row per background job. Return the newest row whose status is new. Write a read-only SQL query or the configured dataframe function.
Input table
| Column | Type | Meaning |
|---|---|---|
job_id | integer | Unique job identifier |
status | text | Job state |
created_on | text | Creation time as fixed-width UTC text (YYYY-MM-DD HH:mm:ss) |
Here, created_on is text, not a timestamp type. Every value is a valid UTC time to the whole second, in the same zero-padded format. So sorting the text in alphabetical order also sorts it by time. Do not convert the values to a local timezone.
Selection rules
- Keep only rows whose status is exactly the lowercase text
new. - Pick the row with the greatest
created_on. - If several matching rows have that same time, pick the one with the smallest
job_id. - Return the columns
job_id,statusandcreated_on, in that order, with at most one row. If no row matches, return an empty table with those columns.
Optimization follow-up
The interviewer asks how to speed up SELECT * FROM job WHERE status = 'new' ORDER BY created_on DESC LIMIT 1 on a table with about 100 million rows, of which about 20,000 are new. Explain which column order in a composite index helps both the equality filter and the ordering. Do this before you suggest a cache.
Only the rows your query returns are checked. You do not need to create an index, and speed and query plans are not measured. The job_id tie rule above makes the result the same every time.
Table schema
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
job
A finite snapshot of job records.
| Column | Type | Nullable | Description |
|---|---|---|---|
| job_idPK | Integer | No | Unique positive job identifier. |
| status | Text | No | Job state; only the exact lowercase value new is selected. |
| created_on | Text | No | A valid UTC creation instant encoded as fixed-width YYYY-MM-DD HH:mm:ss text; lexical order is chronological order. |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| job_id | Integer | No | Unique positive job identifier. |
| status | Text | No | Job state; only the exact lowercase value new is selected. |
| created_on | Text | No | A valid UTC creation instant encoded as fixed-width YYYY-MM-DD HH:mm:ss text; lexical order is chronological order. |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
- The table contains between 0 and 2,000 rows for this exercise.
job_idis unique and is between 1 and10^9, inclusive.- All three columns are non-null.
statuscontains 1 to 20 lowercase ASCII letters or underscores; match onlynew.created_onis text encoding a valid UTC timestamp asYYYY-MM-DD HH:mm:ssat whole-second precision between2000-01-01 00:00:00and2030-12-31 23:59:59, inclusive.- Input rows may appear in any order. Do not mutate the input dataframe.
Similar questions at other companies
- Second Highest SalaryHartford Financial Services · ONSITE INTERVIEWHartford Financial Services interview questions
- In-Memory Database QueriesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Weekend Appointments for Consular Services ReschedulingMcKinsey & Company · OAMcKinsey & Company interview questions
- Benchmarking Tool ReportPalantir · OAPalantir interview questions
- Prepare Taxi Driver Classification DataBoston Consulting Group · OABoston Consulting Group interview questions
- Clean the TapeMillennium · OAMillennium interview questions
- Summarize Customer RecordsAgoda · OAAgoda interview questions
- Merge IntervalsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- Group Transactions by HourAffirm · ONSITE INTERVIEWAffirm interview questions
- Delete Duplicate Employee RecordsInfosys · ONSITE INTERVIEWInfosys interview questions