Embercode
✕
GraphsQuick quiz

Ordering tasks and words

8 questions. Covers Course schedule and alien dictionary in Python.

Questions 1 to 5 of 8

  1. 1. Put the steps of the course-order method (find_order) in the order they run.

    Use the arrows to put the steps in order.

    1. 1.Take a course from the queue, add it to the order, and lower the indegree of the courses that need it
    2. 2.Return the order if every course was taken, otherwise return an empty list
    3. 3.Put every course with indegree 0 into the queue
    4. 4.Count each course's unmet prerequisites (indegree) and build the list of courses that depend on each one
  2. 2. Why does the alien dictionary code register every letter from every word before it reads the neighbouring pairs?
  3. 3. What does this program print? ``python from collections import deque def find_order(num_courses, prereqs): adj = [[] for _ in range(num_courses)] indeg = [0] * num_courses for course, prereq in prereqs: adj[prereq].append(course) indeg[course] += 1 queue = deque(i for i in range(num_courses) if indeg[i] == 0) order = [] while queue: v = queue.popleft() order.append(v) for w in adj[v]: indeg[w] -= 1 if indeg[w] == 0: queue.append(w) return order if len(order) == num_courses else [] print("A:", find_order(4, [[3, 0], [3, 1], [1, 2]])) ``
  4. 4. The last line of find_order was changed to return order. Which statement describes the effect? ``python from collections import deque def find_order(num_courses, prereqs): adj = [[] for _ in range(num_courses)] indeg = [0] * num_courses for course, prereq in prereqs: adj[prereq].append(course) indeg[course] += 1 queue = deque(i for i in range(num_courses) if indeg[i] == 0) order = [] while queue: v = queue.popleft() order.append(v) for w in adj[v]: indeg[w] -= 1 if indeg[w] == 0: queue.append(w) return order ``
  5. 5. Put the steps of alien_order in the order the method runs them.

    Use the arrows to put the steps in order.

    1. 1.Compare each neighbouring pair of words to find the first differing letter
    2. 2.Run Kahn's loop with a queue, and return the letters only if all of them were taken
    3. 3.Add an edge from the earlier letter to the later one, counting it once
    4. 4.Register every letter from every word with indegree 0

3 question(s) left on this page.