Skip to main content
31/31

Find Double-Booked Rooms

Hard

A booking form must warn before saving when two bookings for the same room overlap. Each booking is a map with id, room, startHour and endHour (whole hours; the end hour is not part of the booking, so 9 to 10 and 10 to 11 do not overlap).


Return one map for every pair of overlapping bookings in the same room:

  • room: the room
  • firstId: the id of the booking that comes earlier in the list
  • secondId: the id of the one that comes later

Order the pairs by the position of the first booking, then the position of the second. Each pair appears once. No overlaps returns an empty list.


Why it comes up: this validation compares every booking with every other one. That takes one a!forEach inside another, and inside the inner loop you still need the outer booking.

ri! Rule Inputs

ri!bookingsList of Map
{
  a!map(id: 1, room: "A", startHour: 9, endHour: 11),
  a!map(id: 2, room: "A", startHour: 10, endHour: 12),
  a!map(id: 3, room: "B", startHour: 9, endHour: 10),
  a!map(id: 4, room: "A", startHour: 11, endHour: 13)
}

Example 1: Two overlaps in room A

Input: ri!bookings
{
  a!map(id: 1, room: "A", startHour: 9, endHour: 11),
  a!map(id: 2, room: "A", startHour: 10, endHour: 12),
  a!map(id: 3, room: "B", startHour: 9, endHour: 10),
  a!map(id: 4, room: "A", startHour: 11, endHour: 13)
}
Expected output:
{
  a!map(room: "A", firstId: 1, secondId: 2),
  a!map(room: "A", firstId: 2, secondId: 4)
}

Example 2: Back to back is fine

Input: ri!bookings
{
  a!map(id: 1, room: "A", startHour: 9, endHour: 10),
  a!map(id: 2, room: "A", startHour: 10, endHour: 11)
}
Expected output:
{}

Example 3: Same time, different rooms

Input: ri!bookings
{
  a!map(id: 1, room: "A", startHour: 9, endHour: 12),
  a!map(id: 2, room: "B", startHour: 9, endHour: 12),
  a!map(id: 3, room: "C", startHour: 9, endHour: 12)
}
Expected output:
{}

Example 4: An all-day booking

Input: ri!bookings
{
  a!map(id: 10, room: "R", startHour: 8, endHour: 18),
  a!map(id: 11, room: "R", startHour: 9, endHour: 10),
  a!map(id: 12, room: "R", startHour: 17, endHour: 19),
  a!map(id: 13, room: "Q", startHour: 9, endHour: 10)
}
Expected output:
{
  a!map(room: "R", firstId: 10, secondId: 11),
  a!map(room: "R", firstId: 10, secondId: 12)
}

Example 5: No bookings

Input: ri!bookings
{}
Expected output:
{}

Appian Expression Tips

  • • Use ri!variableName for rule inputs
  • • Use a!localVariables(local!x: …, body) for locals
  • • Use a!forEach(items: …, expression: fv!item) to iterate
  • • Use where() / reject(fn!isnull, list) to filter