Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

Form the adjacency matrix from the data sent , and then run the bfs/dfs on the matrix.

How to create a matrix, is the question here. Since the data sent can be of variable length.

- Arjuna August 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

convert it into a matrix and apply some shortest path algorithm, ex Dijkstra algorithm for directed weighted graph.. If you need a program reply to this comment and do tell me the language you want this solved

- PeyarTheriyaa August 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi There are 2 ways to save,

1) SQL (Structured Data)
Create table
Columns : Source, Destination,Distance

2)NOSQL(in form of json)
Create json{[
{source: A
destination: B,
distance: 6},...]
}

During construction of graph fetch all entries and construct graph.

- sauravomar01 August 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@PeyarTheriyaa Yes,it will be very heplful.I am implementing using spring boot (java 8) So once the request body is sent we need to convert into matrix before saving into DB
Can you pls provide the program,thankyou.

- arvaithi August 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here's the rough idea with half-working code in Python. The idea is simple and mainly involved serializing and deserializing.

import json
import psycopg2

jsonData = '''
{
    "data": [
    {
        "source": "A", 
        "target": "B", 
        "distance": 6
    }, 
    {
        "source": "A", 
        "target": "E", 
        "distance": 4
    }, 
    {
        "source": "B", 
        "target": "A", 
        "distance": 6
    }, 
    {
        "source": "B", 
        "target": "C", 
        "distance": 2
    }, 
    {
        "source": "B", 
        "target": "D", 
        "distance": 4
    }, 
    {
        "source": "C", 
        "target": "B", 
        "distance": 3
    }, 
    {
        "source": "C", 
        "target": "D", 
        "distance": 1
    }
]
}
'''
# Serialize the data
serializedData = json.dumps(jsonData)
# Connect to the database
conn = psycopg2.connect(database=db, user=user, password=password, host=host, port=port)
cursor = conn.cursor()
# Insert the data
cursor.execute('INSERT INTO GRAPH VALUES %s' % (serializedData))

conn.commit()
conn.close()

# Deserialize the data to find shortest path
graphData = json.loads(serializedData)
graph = buildGraph(graphData)
sourceVertex = 'A'
print(dijkstra(graph, sourceVertex))

- prudent_programmer August 06, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More