Compare two expressions
(a+b) + c = c + a + b
-(a+b-c) = c -b -a
Design Twitter trending topics
Design Elevator system
Deep copy of linked list
Compare two expressions
(a+b) + c = c + a + b
-(a+b-c) = c -b -a
Design Twitter trending topics
Design Elevator system
Deep copy of linked list
I was asked to compare two expressions in the interview.
For example, -(a+b+c) = -c-a-b or (a-b)-c = -c + a - b
What is the correct way to tackle this problem.
I did it through looking for expressions and multiple them with the characters in a map
for example
Expression1: a + b -c
before a, it is null so I consider it as a + and put an entry in hashmap as +a with value is 1. before b, there is a plus so the key is +b and value is 1, before c it is - so key in hash is -c and increment the value
For next expression
Expression2: -c + a + b
I do the same but this time I decrease the count as soon as I encounter the same character/digit
public interface IElevator()
{
public boolean GoUP();
public boolean GoDown();
public int GetLevel();
public void SetLevel(int level);
public boolean CanAddLevel(int level);
}
public enum Directions {up, down};
public enum Levels {1,2,3,4};
// this is a normal elevator. We can also implement maintenance elevators or other kinds in similar way
public class Elevator extends IElevator()
{
// Implement Go up
// Implement Go Down
Queue<Integer> elevQ = new LinkedList<Integer>();
// Method to set level
public void SetLevel(int level)
{
elevQ .add(level);
}
// method to get level
public int GetLevel(int level)
{
elevQ .peek();
}
// Method to check if a level can be added
// we can either do this check on controller or give it to the elevators
public boolean CanAddLevel(int level)
{
if(Direction.up && elevQ .peek() < level)
return true;
else if(Direction.down && elevQ .peek() > level)
return true;
return false
}
}
public class ElevatorController()
{
// contains logic to decide which elevator can be called from a particular floor
// Contains a list of elevator
}