Programming Challenge – HackerRank: Friend Circles (DFS in Java)

I got this problem on a HackerRank challenge and this was the best I could think of. Basically only DFS is used to find connected components (in this case groups of friends). It was hard for me to figure out how to track unvisited nodes. Please let me know how I can improve this code. It seems to work, but the test cases given were fairly simple.


There are N students in a class. Some of them are friends, others are not. Their friendship is transitive in nature; H. If A is a friend of B and B is a friend of C, then A is also a friend of C. A circle of friends is a group of students who are friends, directly or indirectly.

You have to perform a function int friendCircles(char()() friends) This returns the number of circles of friends in the class. His argument, friends, is a NxN Matrix consisting of the characters "Y" or "N". If friends(i)(j) == "Y" then ith and jth students are friends with each other, otherwise not. You must return the total number of circles of friends in the class.

Sample Input 0:
Sample Output 0:

Explanation 0:
There are two pairs of friends (0, 1) and (1, 2). So (0, 2) is also a pair of friends by transitivity.
So first friend circle contains (0, 1, 2) and second friend circle contains only student 3

Sample Input 1:
Sample output 1:

Restrictions (sorry, formatting could not be performed, so I had to set the restrictions here):

  • 1 <= N <= 300.
  • Each element of matrix friends is "Y" or "N".
  • The number of rows and columns is the same for friends.
  • Friends (i) (j) = "Y", where 0 <= i <N.
  • Friends (i) (j) = Friends (j) (i), where 0 <= i <j <N.


import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashSet;
import java.util.Set;

public class Solution {
    public static int friendsCircle(char()() friends) {
        // The only alternative I could think of, instead of
        // tracking unvisited nodes, was to put visited nodes
        // in a set and then do setOfAllNodes.removeAll(visited)
        // to see which nodes are still unvisited
        Set unvisited = new HashSet<>();
        boolean() visited = new boolean(friends.length);
        Deque stack = new ArrayDeque<>();
        int connectedComponents = 0;
        for (int i = 0; i < friends.length; i++) {
        // dfs on friends matrix
        while (!unvisited.isEmpty()) {
            while (!stack.isEmpty()) {
                int currVertex = stack.pop();
                if (visited(currVertex) == false) {
                    visited(currVertex) = true;
                    for (int i = 0; i < friends(currVertex).length; i++) {
                        if (friends(currVertex)(i) == 'Y' && visited(i) == false) {
        return connectedComponents;

    public static void main(String() args) {
        char()() friends = {

Can my friend use Android Launcher 3 to unlock or get on my phones?

I left my two phones in my girlfriend's car and when I checked my google activity, appeared several times. In addition, other activities such as Facebook, Text Now and other apps were displayed by my cell phones during this time. Could it be that she's hacking into my phones, maybe even with her own phone, or could it be from the Playstore that updates my apps automatically?

[ Politics ] Open question: The number of Shiite Americans killed is a drop in the bucket compared to the thousands of Americans killed by our Saudi Sunni oil friend?

… mates. Isn't Iran's main sin Israel's biggest regional enemy, but far worse if you don't play with Exxon / Mobile and compete for US oil companies (and Russians)? Anyone who disagreed with your stupid Iraq war was also a "terrorist lover" that I still remember. It's been great for 20 years, and there is no end in sight, as we said much worse.

Visa – proof of relationship when visiting a friend in Canada

So far I was able to go through the documentation until I came to the following conclusion: – Proof of relationship, I will visit a friend, after the tour I return home.

How can I show the visa officer that this is my friend? I have no records of him, I just stated that I got a letter from him, and when I see this proof of relationship, I'm a little confused. I decided to ask here to clarify this point.
In this case, how can I prove a relationship?

Applications – Add a friend in the online app by username without ID or phone number

My name is Husam from Egypt
I know you don't even have time to read my message, but I know you have a real feeling in love.
I need your help. I added an Asian girl from Hong Kong to a friend. In line app
I lose the connection with her because when I created new software for my phone, everything disappeared from the line app and I could not add her anymore, her name is 趙如萱
And that's her profile.
She doesn't know my ID, my phone number or me
You will find me please ..
It is a matter of life and death for me and herEnter image description here

UK – Applying for a tourist visa sponsor by my British friend from Scotland in the Philippines and unemployed

Hello, hello, Ma'am / sir

I just want to ask if I can get a normal tourist visa, even if I'm unemployed. I live in the Philippines and I have my longtime friend for almost 4 years and we have already met a visa as my sponsor, if there is the possibility to get a standard visa, what conditions do I need, even if I am unemployed I have to to submit all papers of the sponsor? is that good enough?

My drunken friend

One of my friends is always drunk. Sometimes I'm a bit confused, whether he's drunk or not. One day I talked to him about his drinks! He began to describe his way of drinking. Let me share his ideas a bit. I express it in my words.

There are many types of drinks that he used to drink. But there are some rules; There are some drinks that have some requirements. Suppose, if you want to take wine, you should have taken soda and water before. That's why it's not so easy to get drunk right.

Now given the name of a few drinks! And to the requirements of the drinks you have to say, whether you can get drunk or not. To get drunk, a person should take all drinks.

The input begins with an integer T (≤ 50) indicating the number of test cases.

Each case starts with an integer m (1 ≤ m ≤ 10000). Each of the next m lines contains two names in the format a b, which means that you must have a before you have b. The names contain at most 10 characters without spaces.

Give the case number and "yes" or "no" for each case, depending on whether you can get drunk or not.

Sample Input
soda wine
water wine
soda wine
water wine
wine water

Output for Sample Input:
Case 1: Yes
Case 2: No