Friends

The questions below are due on Sunday November 15, 2026; 11:59:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.
Back to Exercises

Notes:

  • Assume friend is a reserved keyword in C for this problem. You should not use it as your variable name.
  • The checkers here require that you manually cast void* pointers from malloc to the type you want
    (e.g. int *ptr = (int *) malloc(sizeof(int) * 3);).
  • Looking at the test cases (by clicking Submit) is a good way to understand what the problems expect of you.

Let's make a social media app! Our app is very simple. It has four main functionalities, and nothing else:

  • Registering a new person.
  • Adding two people as friends. (Friendship is bidirectional.)
  • Checking whether two people are friends.
  • Getting a list of mutual friends.

We will represent each person with a struct containing the person's name, number of friends, maximum number of friends, and a list of friends.

For the friend list, we have an array of person_t pointers. A person_t pointer has type person_t *, so an array of person_t pointers has type person_t **. (In the definition of the struct, however, we cannot directly specify person_t ** as the type so we use struct person ** instead.)

The array will be dynamically allocated and will have the size of friends_limit. As you will see in Section 2, every time the limit is hit, we will reallocate the array and change the limit.

// DEFAULT_FRIENDS_LIMIT is defined as a positive int constant
typedef struct person {
  char *name; // allocated array of characters including null character
  struct person **friends; // dynamically allocated array of pointers to person_t*
  int num_friends; // number of valid person_t* entries in `friends` (indexed 0 to num_friends-1)
  int friends_limit; // the current allocated size of `friends` (number of `person_t` that can be stored)
                     // 0 <= num_friends <= friends_limit
} person_t;

We will implement these functions. Each will operate on pointers to person_t rather than person_t directly, because some functions (like addFriend) will have to mutate the struct.

// make a person struct and return the pointer to that struct
person_t *makePerson(char *name);

// add p1 and p2 to each other's friend list
void addFriend(person_t *p1, person_t *p2);

// return 1 if p1 and p2 are friends, 0 otherwise
int areFriends(person_t *p1, person_t *p2);

// return the number of friends p1 and p2 have in common
int countMutualFriends(person_t *p1, person_t *p2);

// return a (dynamically allocated) list of mutual friends
person_t **listMutualFriends(person_t *p1, person_t *p2);

Registering a person

To register a new person, we could try to do something like this.

// make a person struct and return the pointer to that struct
person_t *makePerson(char *name) {
  person_t p; // make a person

  p.name = (char *) malloc(strlen(name) + 1); // allocate enough space to copy the name (including the null terminator)
  strcpy(p.name, name); // we copy the name instead of using the name directly to make sure that no one changes `name` under our nose

  p.num_friends = 0; // start with no friends
  p.friends_limit = DEFAULT_FRIENDS_LIMIT; // default to a positive constant
  p.friends = (person_t **) malloc(sizeof(person_t*) * p.friends_limit); // dynamically allocate a friend list

  return &p; // return the pointer to this person
}

However, if we try to use this function in our code (say, in main), we might run into issues! See this example:

int main(void) {
  char nameBuffer[100];
  strcpy(nameBuffer, "joe")
  person_t *joe = makePerson(nameBuffer);
  // (... do some random stuff ...)
  // (... call some functions ...)
  strcpy(nameBuffer, "silvina")
  person_t *silvina = makePerson(nameBuffer);
  // We defined `displayPerson(person_t)` (outside this code) to test the correctness of the functions.
  displayPerson(joe);
  displayPerson(silvina);
}

In this example, the code might crash when trying to display joe. Or worse: the displayed result for both joe and silvina might have the same name, "silvina", even though they are supposed to be different people!

This happens because p in makePerson is only a local variable. When makePerson returns the address to p, that pointer remains valid only as long as the function is still in scope (i.e. when the function is still running). So, after the function call, joe and silvina are actually pointing to something that might be overwritten by other parts of the system. Overall, the behavior here is unpredictable.

In Assembly-speak, you can think of p as a block of memory allocated on the stack at the start of the function call. At the end of the function call, that block is deallocated. The stack space may be reallocated by other unrelated functions and thus is overwritten with "junk values."

To fix this, we have to use malloc to dynamically allocate space for storing a person, then set up the information for that person, and finally return the pointer to that struct. Things that are dynamically allocated will remain valid out of scope, so it is totally fine to return the pointer. The data will stay there as long as we have not freed the pointer using free.

Your task is to rewrite the function makePerson such that it dynamically allocates space for a person instead of using a local variable. This should make joe and silvina in the example code earlier point to distinct, valid structs. You may assume that the heap has enough space available to allocate the requested number of bytes in making calls to malloc (assuming you are requesting the correct number of bytes according to the problem specifications, of course).

Reminder that the checkers here require that you manually cast void* pointers from malloc to the type you want (e.g. int *ptr = (int *) malloc(sizeof(int) * 3);)

Adding a friend

Now that makePerson actually makes a new person, it's time to get people to socialize. We want to write a function addFriend.

// add p1 and p2 to each other's friend list
void addFriend(person_t *p1, person_t *p2);

The function should add p2 to the end of p1's list of friends and vice versa. It should also increment num_friends accordingly. The function will allow us to do something like this in the rest of our code:

int main(void) {
  person_t *joe = makePerson("joe");
  person_t *silvina = makePerson("silvina");
  person_t *c = makePerson("c");
  person_t *python = makePerson("python");
  person_t *java = makePerson("java");

  addFriend(joe, silvina);
  addFriend(c, python);
  addFriend(c, java);
  addFriend(java, python);
}

However, there's a catch! Sometimes, adding a new friend is not possible because one or both people have reached their limits on the number of friends. To counter this, whenever a person is about to exceed the limit, we double the limit for that person. This means we will have to allocate a new space so friends can store a larger number of people. (If we try to write past the original bound, the behavior is undefined and the program might crash.) This can be done by using malloc to reallocate a new space, copying all data over, and then making friends point to the new array. Don't forget to free the old array as well.

You may assume p1 and p2 are not the same person, and that they have not been friends before.

Checking friendships

Now, we can check whether two people are friends or not by iterating through the friend list.

Mutual friends

Finally, any reasonable social media should allow two people to find whether they have any friends in common.

Counting mutuals

First, you should write a function that counts the number of mutual of friends.

List all mutuals

Using the function you just wrote, allocate an array of person_t * to store the list of mutual friends. Mutual friends should be listed in the order they appear in p1's friend list.

Back to Exercises