ThoughtWorks Interview Question for SDE1s


Country: India
Interview Type: In-Person




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

c# implementation.

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;

namespace PhoneBook {

    public struct Person {

        public Person( string name, string phoneNumber, string address ) {
            Name = name;
            PhoneNumber = phoneNumber;
	    Address = address;
        }

        public string Name { get; }
        public string PhoneNumber { get; }
        public string Address { get; }
    }

    public class Phonebook {

        private readonly object _lock = new object();

        private readonly ConcurrentDictionary<string, Person> _indexByName;

        private readonly ConcurrentDictionary<string, Person> _indexByPhoneNumber;

        public Phonebook( IEnumerable<Person> people ) {
        
            _indexByName = new ConcurrentDictionary<string, Person>();
            _indexByPhoneNumber = new ConcurrentDictionary<string, Person>();

            foreach ( var p in people ) {
                _indexByName.AddOrUpdate( p.Name, p, (s, pers) => pers );
                _indexByPhoneNumber.AddOrUpdate( p.PhoneNumber, p, (s, pers) => pers );
            }
        }

        public Person? LookupByName( string name ) {

            Person pers;
            var res = _indexByName.TryGetValue( name, out pers );
            return res ? (Person?)pers : null;
        }

        public Person? LookupByPhoneNumber( string phoneNumber ) {

            Person pers;
            var res = _indexByPhoneNumber.TryGetValue( phoneNumber, out pers );
            return res ? (Person?)pers : null;
        }

        public void AddPerson( Person person ) {

            Monitor.Enter( _lock );

            var res1 = LookupByName( person.Name );
            var res2 = LookupByPhoneNumber( person.PhoneNumber );

            if ( res1 != null && res2 != null ) {
                Monitor.Exit( _lock );
                return;
            }

            Person p;

            if ( res1 != null ) {
                
                _indexByPhoneNumber.TryRemove( ((Person)res1).PhoneNumber, out p );
                _indexByPhoneNumber.TryAdd( person.PhoneNumber, person );
            }
            
            if ( res2 != null ) {

                _indexByName.TryRemove( ((Person)res2).Name, out p );
                _indexByName.TryAdd( person.Name, person );
            }

            _indexByName.AddOrUpdate( person.Name, person, (s, pers) => person );
            _indexByPhoneNumber.AddOrUpdate( person.PhoneNumber, person, (s, pers) => person );

            Monitor.Exit( _lock );
        }
    }
}

- zr.roman December 29, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;

namespace Employee
{
    public class Contact
    {

        public string Name { get; private set; }
        public string PhoneNumber { get; private set; }
        public string Address { get; private set; }

        public Contact(string name, string phoneNumber, string address)
        {
            this.Name = name;
            this.PhoneNumber = phoneNumber;
            this.Address = address;
        }
    }

    public class PhoneBook
    {
        private readonly IEnumerable<Contact> _contact;

        public PhoneBook(IEnumerable<Contact> contact)
        {
            this._contact = contact;
        }
        public IEnumerable<Contact> lookForName(string name)
        {
            var lookup = _contact.ToLookup(nd => nd.Name);
            return lookup[name];
        }
        public IEnumerable<Contact> lookForPhoneNumber(string phoneNumber)
        {
            var lookup = _contact.ToLookup(nd => nd.PhoneNumber);
            return lookup[phoneNumber];
        }
    }
}

- Ramkumar January 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;

namespace Employee
{
    public class Contact
    {

        public string Name { get; private set; }
        public string PhoneNumber { get; private set; }
        public string Address { get; private set; }

        public Contact(string name, string phoneNumber, string address)
        {
            this.Name = name;
            this.PhoneNumber = phoneNumber;
            this.Address = address;
        }
    }

    public class PhoneBook
    {
        private readonly IEnumerable<Contact> _contact;

        public PhoneBook(IEnumerable<Contact> contact)
        {
            this._contact = contact;
        }
        public IEnumerable<Contact> lookForName(string name)
        {
            var lookup = _contact.ToLookup(nd => nd.Name);
            return lookup[name];
        }
        public IEnumerable<Contact> lookForPhoneNumber(string phoneNumber)
        {
            var lookup = _contact.ToLookup(nd => nd.PhoneNumber);
            return lookup[phoneNumber];
        }
    }
}

- nkdram January 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

using System;
using System.Collections.Generic;
using System.Linq;

namespace Employee
{
    public class Contact
    {

        public string Name { get; private set; }
        public string PhoneNumber { get; private set; }
        public string Address { get; private set; }
        public string _All
        {
            get
            {
                return string.Format("{0}:{1}:{2}",
                    this.Name,
                    this.PhoneNumber,
                    this.Address).ToLower();
            }
        }

        public Contact(string name, string phoneNumber, string address)
        {
            this.Name = name;
            this.PhoneNumber = phoneNumber;
            this.Address = address;
        }

       
    }

    public class PhoneBook
    {
        private readonly IEnumerable<Contact> _contact;

        public PhoneBook(IEnumerable<Contact> contact)
        {
            this._contact = contact;
        }
        public IEnumerable<Contact> lookForName(string name)
        {
            var lookup = _contact.ToLookup(nd => nd.Name);
            return lookup[name];
        }
        public IEnumerable<Contact> lookForPhoneNumber(string phoneNumber)
        {
            var lookup = _contact.ToLookup(nd => nd.PhoneNumber);
            return lookup[phoneNumber];
        }

        public IEnumerable<Contact> lookForAlphabet(char letter)
        {
            var lookup = _contact.ToLookup(p => Convert.ToChar(p.Name.Substring(0, 1)),
                  p => p);
            return lookup[letter];
        }

        public IEnumerable<Contact> lookForAll(string anyVal)
        {
            var lookup = _contact.Where(p => p._All.Contains(anyVal.ToLower()));
            return lookup;
        }
    }

    class CustomComparer : IEqualityComparer<Contact>
    { 
    
    }
}

- nkdramkumar January 03, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

class PersonDetails{
String Name;
long PhoneNumber;
String Email;
int EmployeeId;

public void PersonDetails(String Name, long PhoneNumber, String Email, int EmployeeId){
this.Name = Name;
this.PhoneNumber = PhoneNumber;
this.Email = Email;
this.EmployeeId = EmployeeId;
}

public void PersonDetails(){
this.Name = null;
this.PhoneNumber = 0;
this.Email = null;
this.EmployeeId = 0;
}

public String getName(){
return this.Name;
}

public long getPhoneNumber(){
return this.PhoneNumber;
}

public String getEmail(){
return this.Email;
}

public int getEmployeeId(){
return this.EmployeeId;
}
}

class SearchResult{

public void SearchResult(String Name, HashMap<Integer, Object> map){
Iterator it = map.keySet().iterator();
while(it.hasNext()){
int key = (int) it.next();
PersonDetails PDet = (PersonDetails) map.get(key);
if(PDet.getName().equalsIgnoreCase(Name)){
System.out.println("\n Name: "+PDet.getName());
System.out.println("\n PhoneNumber: "+PDet.getPhoneNumber());
System.out.println("\n EmailID: "+PDet.getEmail());
System.out.println("\n EmployeeID: "+PDet.getEmployeeId());
System.out.println("\n");
System.out.println("\n");
}
}
}

- Hemanth January 04, 2016 | 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