• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

HackeEarth Flipkart’s Drone

November 24, 2014 by Dhaval Dave

HackeEarth Flipkart’s Drone : After listening to the news of testing of Delivery Drone
But this was only possible if two of the shipping addresses had “V1-type” road connecting them(V1-type roads are the fastest in the city). So, Sachin and Binny ask you for a little help. They want you to tell them if they could send two orders together for two given locations or not.
Read Question at :http://www.hackerearth.com/problem/algorithm/sachins-drones/Input
First line of the input contains an integer T, denoting the number of test cases for your problem. Then T test cases follow. First line of each test case contains two integers N and M, denoting number of shipping addresses and the number of V1-type roads. Then M lines follow each containing two space separated integers A and B, denoting that there is a V1-type road between locations A and B. Assume that locations are indexed by numbers from 0 to N-1.
Next line contains an integer Q denoting the number of queries made by Flipkart founders to you. Each of the next Q lines contain two integers X and Y. For each query you have to find out if orders meant for shipping addresses X and Y can be sent together or not.
Note that there might be multiple V1-type roads between same pair of locations, also there might be a V1-type road that links a location to itself.Output
For each test case print Q lines – one for each query. Output “YES” if the orders are to be delivered together and “NO” otherwise.

Read this Books on Java to clear your concept.

Constraints
1 <= T <= 200
1 <= N <= 200
1 <= M <= 3000
0 <= A, B, X, Y <= N-1
1 <= Q <= 10000

Sample Input (Plaintext Link)
1
4 2
0 1
1 2
3
0 2
0 3
2 1

Sample Output (Plaintext Link)
YES
NO
YES

Solution 1 : Graph Based Solution (DFS)

#include <iostream>
#include <list>

using namespace std;

#define gc getchar_unlocked
#define pc putchar_unlocked
inline int scan(){register int n=0,c=gc();while(c<'0'||c>'9')c=gc();while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=gc();return n;} 

class Graph
{
    int V;    // No. of vertices
    list<int> *adj;    // Pointer to an array containing adjacency lists
    int DFSUtil(int a ,int b, bool visited[]);  // A function used by DFS
public:
    Graph(int V);   // Constructor
    void addEdge(int a ,int b);   // function to add an edge to graph
    int DFS(int a ,int b);    // DFS traversal of the vertices reachable from v
};

Graph::Graph(int V)
{
    this->V = V;
    adj = new list<int>[V];
}

void Graph::addEdge(int v, int w)
{
    adj[v].push_back(w);
    adj[w].push_back(v);
}

int Graph::DFSUtil(int a, int b, bool visited[])
{
    // Mark the current node as visited and print it
    int ret=0;
    visited[a] = true;
    //out <<"Nodes= " << a <<"n";
  if(a == b) return 1;
    // Recur for all the vertices adjacent to this vertex
    list<int>::iterator i;
    for(i = adj[a].begin(); i != adj[a].end(); ++i)
        if(!visited[*i]){
            ret = DFSUtil(*i,b, visited);
            return ret;
            }

}
int Graph::DFS(int a, int b)
{
    // Mark all the vertices as not visited
    bool *visited = new bool[V];
    int ret=0;
    for(int i = 0; i < V; i++)
        visited[i] = false;
    ret = DFSUtil(a,b,visited);
    return ret;
}

int main()
{
    // Create a graph given in the above diagram
    int t,A,B,N,M,Q;
    t=scan();
    Graph g(200);
    while(t--){
    N=scan();
    M=scan();
    while(M--){
    A=scan();
    B=scan();
    if(A<N && B<N) g.addEdge(A,B);
    }
    }
    //
    Q=scan();
    while(Q--){
    A=scan();
    B=scan();
    if(A<N && B<N){
      if(g.DFS(A,B)==1)cout <<"YESn";
      else cout << "NOn";
    }
    else cout << "NOn";
    }

    return 0;
}

See Working Code at : http://ideone.com/d9hKUQ

Solution 2 : Array Based Solution

#include <iostream>
#include <list>
using namespace std;
#define gc getchar_unlocked
#define pc putchar_unlocked
inline int scan(){register int n=0,c=gc();while(c<'0'||c>'9')c=gc();while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=gc();return n;} 

bool findPath(int X, int Y, bool V[],int T[],int N){
if(T[X]==Y) {
//cout<<X<<Y<<"n";
return true;
}
else if(V[X]==0 && T[X]!=Y && T[X]<N ){
//cout << "temp="<<X<<"-"<<T[X]<<"n";
V[X]=1;
return findPath(V[X],Y,V,T,N);
}
else return false;
}
int findPathUtil(int X, int Y,int A[], int B[],int N){
bool VA[200]={0};
bool VB[200]={0};
bool ans = false;
//char a='a',b='b';
ans = ans || findPath(X,Y,VA,A,N) || findPath(X,Y,VB,B,N);
return ans;
}

int main()
{
    int t,N,M,Q,nn,mm,X,Y;
    t=scan();
    int A[200],B[200];
    while(t--){
    N=scan();
    M=scan();
    //int A[N],B[N];
    mm=M;
    while(M--){
    int t1=scan();
    int t2=scan();
    A[t1]=t2;
    B[t2]=t1;
    //added all entries in A,B array
    }
    }
    /*for(int i=0;i<mm;i++){
    cout << i << "=" <<A[i]<<"n";
    }*/

    Q=scan();
    while(Q--){
    X=scan();
    Y=scan();
    if(X<N && Y<N){
      if(findPathUtil(X,Y,A,B,N)==true)cout<<"YESn";
      else cout <<"NOn";
    }
    else cout << "NOn";
    }
    return 0;
}

 

See Working Code at http://ideone.com/AY4Uqr

Waiting from you to get more optimal solution

Similar Articles

Filed Under: Flipkart Interview Questions, Hacker Earth Questions, Interview Questions, problem Tagged With: Array

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Printing Longest Common Subsequence

Check if an array has duplicate numbers in O(n) time and O(1) space

System Design: Designing a LLD for Hotel Booking

Doubly linked list

CodeChef’ RRCOPY

Level order traversal in Spiral form

robot standing at first cell of an M*N matrix. It can move only in two directions, right and down. In how many ways, it can reach to the last cell i.e. (M, N) Code it

Handle duplicates in Binary Search Tree

Right view of Binary tree

Trie Dictionary

Generate largest number arranging a no. of given non negative integer numbers

Count number of ways to reach a given score in a game

C Program for TAIL command of UNIX

K’th Largest Element in BST when modification to BST is not allowed

Coin Collection Dynamic Programming

Linked List V/S Binary Search Tree

Length of the longest substring without repeating characters

Amazon Interview Experience – SDE Chennai

Given a float number convert it into the string WITHOUT using any inbuilt Function

How strtok() Works

Leetcode: Merge Intervals

Stickler thief

Get K Max and Delete K Max in stream of incoming integers

BlueStone E-commerce Interview Experience

VMWare Openings

Test Cases for Round Function

Max Sum in circularly situated Values

flattens 2 D linked list to a single sorted link list

Longest Increasing Subsequence

Maximum of all subarrays of size k

Copyright © 2025 · Genesis Framework · WordPress · Log in