← QCE Digital Solutions Unit 3

Merging two data sources in pseudocode

Loops through one data source, finds the record with the matching key in the other, then combines them or appends unmatched records, avoiding duplicate keys.

Understanding Merging two data sources in pseudocode in QCE Digital Solutions Unit 3

What it does: Handling records with no match means adding them to the merged result so no data is lost. Checking the key before appending stops the same key appearing twice in the merged result. A merge works by matching on a key field. Loop through one source and search the other for the same key. The key must appear in both sources and identify the same entity. studentId does this; mark does not. Comparing uses a comparison operator such as =. The arrow <- is for assignment, which would overwrite a value instead of testing it. Comparing the key fields (here studentId) is how the merge decides that two records describe the same thing.

Common mistakes: The score must be copied from the matching record in the other source (b.score). Assigning a.score to itself changes nothing, so nothing is merged. It should be a.score <- b.score. The inner loop must search the other data source, listB, for the matching key. Looping through listA again only compares listA with itself. It should be for each b in listB.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 194–250) and word search (Modules 194–337).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which describes the basic merge algorithm for two data sources?
Join the two sources end to end without checking any keysLoop through one source and look up the record with the matching key in the other
Matches
for each s in students append s to merged append r to merged
↓
Both records are appended without matching on the key, so the same student appears twice with duplicate keys instead of one combined record
Applying
Meanings
Term
merge algorithm
Meaning
Loops through one data source, finds the record with the matching key in the other, then combines them or appends unmatched records, avoiding duplicate keys.
Practice
for each n in newRecords
Testing
Gaps
if a.id b.id then
Check Answer
Spots
3 if a.id = b.id then
4 a.score <- a.score

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Zebra' badge — Pattern Matcher, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which describes the basic merge algorithm for two data sources?
A. Loop through one source and look up the record with the matching key in the other ✓
B. Join the two sources end to end without checking any keys
A merge works by matching on a key field. Loop through one source and search the other for the same key.
2. A student has two lists, students and results, both with a studentId. Which should be the key field for the merge?
A. mark, because it is a number
B. studentId, because it identifies the same person in both sources ✓
The key must appear in both sources and identify the same entity. studentId does this; mark does not.
3. A record in source A has key 7, but source B has no record with key 7. What should the merge do?
A. Append the unmatched record to the merged result (or handle it by a clear rule) ✓
B. Ignore the problem and let the program crash
Records with no match must be handled deliberately. Typically you append them, or log or skip them, but never leave this case undefined.
4. Which pseudocode line correctly tests whether two records match on the key?
A. if a.id <- b.id then
B. if a.id = b.id then ✓
Comparing uses a comparison operator such as =. The arrow <- is for assignment, which would overwrite a value instead of testing it.
5. What is the risk if you append every record from source B without checking whether its key is already in the merged result?
A. Duplicate keys appear in the merged result ✓
B. The result will be sorted in the wrong order
Appending without checking makes matched records appear twice. Only append records whose key has no match.

Match the Pairs

for each s in students → Loops through every record in the first data source so each one can be checked against the second source
if s.studentId = r.studentId then → Matches records from the two sources on the key field
A record with no match in the other source is appended to the merged result instead of being dropped → if found = false then append s to merged
Before adding a record, check that its key is not already in the merged result, so there are no duplicate keys → if key not in merged then append record to merged
for each s in students append s to merged append r to merged → Both records are appended without matching on the key, so the same student appears twice with duplicate keys instead of one combined record
for each s in students if s.studentId = r.studentId then append combined to merged endif next s → There is no handling for records with no match, so unmatched students are lost from the merged result

Key Term

merge algorithm
Loops through one data source, finds the record with the matching key in the other, then combines them or appends unmatched records, avoiding duplicate keys.

Fill in the Blanks

1. for each a in sourceA for each b in sourceB if a.id = b.id then
Hint: Type the comparison operator that checks whether the two key fields are equal.
2. found ← false if found = false then append b to merged
Hint: Type the Boolean value that means no matching record was found.
3. if a.id = b.id then newRec ← combine(a, b) append newRec to merged
Hint: Type the function name that joins the two matching records into one.
4. for each b in sourceB if not exists(b.id, merged) then append b to merged
Hint: Type the Boolean operator that stops a key already in the merged result being added again.
5. merged ← [] for each a in sourceA append a to merged
Hint: Type the keyword phrase that loops through every record in the first data source.

Spot the Error

Question 1
A for each a in listA
B for each b in listB
C if a.id = b.id then
D a.score <- a.score
E end if
Line D — The score must be copied from the matching record in the other source (b.score). Assigning a.score to itself changes nothing, so nothing is merged. It should be a.score <- b.score.
Question 2
A for each a in listA
B found <- false
C for each b in listA
D if a.id = b.id then
E found <- true
F end if
Line C — The inner loop must search the other data source, listB, for the matching key. Looping through listA again only compares listA with itself. It should be for each b in listB.
Question 3
A for each n in newList
B if n.id not in master then
C append n to newList
D end if
Line C — An unmatched record has to be added to the merged data source (master). Appending it to newList, the list being looped through, does not merge anything. It should be append n to master.
Question 4
A for each n in newList
B if n.id in master then
C append n to master
D else
E append n to master
F end if
Line C — When the key already exists in master, appending the record creates a duplicate key. A matched record should update or combine with the existing record, for example update master record.
Question 5
A merged <- empty array
B for each a in merged
C for each b in listB
D if a.id = b.id then
E append a + b to merged
F end if
Line B — merged starts empty, so looping through it never runs and nothing is ever merged. The outer loop must go through the source data, for each a in listA.

Student with a class code? Join your class →